Eric R Eric R - 5 months ago 8
JSON Question

remove one object inside json array and return the result

I have some objects in JSON array

var names = [
{ id:"101", name:"eric" },
{ id:"202", name:"john" },
{ id:"303", name:"zack" },
{ id:"404", name:"ron" }
];


and i want to remove one of the object and i have only one data to check the object array, which is the id = 202 and return it like this

[
{ id:"101", name:"eric" },
{ id:"303", name:"zack" },
{ id:"404", name:"ron" }
];


can anyone help me ?

Thank you for the help :)

Answer

With the use of filter:

var res = names.filter(function(obj) {
    return obj.id !== "202";
});

console.log(res);
Comments