I would like to know how to remove particular key in object array javascript.
In the array object obj, if the id is null remove the key in javascript
var obj = [{
id: 1,
field: "finance"
}, {
id: null,
field: "service}, {
id: 2,
field: "information"
}]
functionremoveKey(arrobj) {
return arrobj.filter(e => {
if (e.id == null) {
delete e.id;
}
}
}
var result = removeKey(obj);
Expected Output:
{
{ id: 1, field: "finance" },
{ field: "service" },
{ id: 2, field: "information" }
]
.filter()is meant to be used for/as. You either want.forEach()or.map()service. Is it a typo?obj.forEach(e => { if (e.id === null) { delete e.id } });