Reputation: 179
I have this array:
var myArray = [
{first_name: "Oded", last_name: "Taizi", id: 1},
{first_name: "Ploni", last_name: "Almoni", id: 2}
];
An i want to remove the id element? I create function like this but it doesn't work correctly.
function removeKeys(array,keys){
for(var i=0; i<array.length; i++){
for(var key in array[i]){
for(var j=0; j<keys.length; j++){
if(keys[j] == key){
array[i].splice(j, 1);
}
}
}
}
}
removeKeys(myArray ,["id"]);
The result array should look like:
[
{first_name: "Oded", last_name: "Taizi"},
{first_name: "Ploni", last_name: "Almoni"}
];
Upvotes: 3
Views: 25288
Reputation: 76
Maybe with a filter on the array:
filteredArray = myArray.filter(item => return (item.id !== id));
This will return a new array without the matching element.
Upvotes: 4