I have a JavaScript array with objects and an array having some Ids. What I want is to compare the objects in the array with the array of ids and if any id is found in an object, I want to remove that element from the array. Doing this the result shows undefined in place of deleted element.
var data = [{"name": "John_Smith","val":"3","id":"2"},{"name": "Peter_Adams","val":"2","id":"3"},{"name": "Priya_Shetye","val":"1","id":"4"},{"name": "Sara_Brown","val":"4","id":"5"}]
var arr = ["2","5"];
for (var i = 0; i < data.length; i++) {
for(var j=0;j<arr.length;j++){
if (arr[j]==data[i].id ) {
delete data[i];
}
}
}
The result shows [undefined,object object,object object,undefined]
. Is there any way to get only [object object,object object]
?