2
votes

I have a JSON array:

var arr = [
{ID: "1", Title: "T1", Name: "N1"}, 
{ID: "2", Title: "T2", Name: "N2"}, 
{ID: "3", Title: "T3", Name: "N3"}
]

How can I delete the Title key from all the rows without a loop cycle?

Result should look like:

var arr = [
{ID: "1", Name: "N1"}, 
{ID: "2", Name: "N2"}, 
{ID: "3", Name: "N3"}
]

I tried the following:

delete arr.Title

but it results a logical response "true" instead of an array.

6
That's a wrong js object, you meant array []?Ele
Yes, array [{}, ... ] will edit the questionHCMan
processing an array is by it's very nature an iterative task. Whether it's explicit in a for loop or implicit in a map or reduce operation, those are all iterative, or what you are calling loop cycle. Why is it necessary to not to use some sort of iteration? Unless this is purely academic, then fine. One other thing, JSON is a string representation of an Object. What you have is an object literal or an array of object literals. The true result is not very helpful. delete {}.abcdefg will return true as well.gkelly

6 Answers

4
votes

you weren't so far:

[edit] with alternatives solutions Combine 1) forEach / for..of. 2) delete / Reflect.deleteProperty

let
  arr_1 = [
    {ID: 1, Title: "T1", Name: "N1"}, 
    {ID: 2, Title: "T2", Name: "N2"}, 
    {ID: 3, Title: "T3", Name: "N3"}
  ],
  arr_2 = arr_1.map(e=>Object.assign({},e))  // new array of copies
;

// solution 1
arr_1.forEach(elm=>delete elm.Title)

// solution 2
for(let elm of arr_2){ Reflect.deleteProperty(elm, 'Name') } // changing

console.log('arr_1 =', JSON.stringify(arr_1))
console.log('arr_2 =', JSON.stringify(arr_2))
2
votes

An alternative would be the function map to create a new array with the desired output. Inside of the handler, create a new Object using the object from each index and finally delete the undesired property name Title.

Assuming you meant array, this approach doesn't mutate the original objects.

let arr = [{ID: "1", Title: "T1", Name: "N1"}, {ID: "2", Title: "T2", Name: "N2"}, {ID: "3", Title: "T3", Name: "N3"}],
    result = arr.map(o => {
      let obj = Object.assign({}, o);
      delete obj.Title;
      return obj;
    });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
1
votes

You want to do this without a loop. I'm not sure what you mean. We can certainly write code that doesn't explicitly use for in order to loop, if that's what you want. But JS, like most languages (there are exceptions such as APL, and K), does not offer any way to operate directly on the elements of the list en masse. So, you can abstract the looping with map. But there is still likely some looping under the hood.

var arr = [
  {ID: "1", Title: "T1", Name: "N1"}, 
  {ID: "2", Title: "T2", Name: "N2"}, 
  {ID: "3", Title: "T3", Name: "N3"}
]

const newArr = arr.map(({Title, ...rest}) => ({...rest}))

console.log(newArr)
0
votes

Your variable arr is not an array. It is an object.
Objects are surrounded by curly braces {}, like this: { "name":"Bob", "alive":true, "dead":false }
An array is surrounded by brakets [], like this: ["Bob","Jane","Mary]

0
votes

For actual JSON string, the JSON.parse reviver parameter can be used to filter or modify values :

var json = '[{"ID":"1","Title":"T1","Name":"N1"},{"ID":"2","Title":"T2","Name":"N2"},{"ID":"3","Title":"T3","Name":"N3"}]'

var arr = JSON.parse(json, (k, v) => k != 'Title' ? v : k[-1]);

console.log( arr );
0
votes

You can accomplish the solution using functional utilities that give you to solve the things in easier way.

import { map, omit } from 'lodash/fp';
const newArr = map(omit('Title'), arr);