Reputation: 33
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.
Upvotes: 3
Views: 8170
Reputation: 22866
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 : void 0);
console.log( arr );
Upvotes: 1
Reputation:
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);
Upvotes: 0
Reputation: 22265
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))
Upvotes: 5
Reputation: 50787
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)
Upvotes: 1
Reputation: 664
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]
Upvotes: 0
Reputation: 33726
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; }
Upvotes: 3