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 Answers 6
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))
3 Comments
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; }
4 Comments
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)
2 Comments
omit functions, and I personally use Ramda's, but there is little obvious reason to bring one in here, unless you expect to reuse it.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 );
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]
2 Comments
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);
[]?delete {}.abcdefgwill return true as well.