Check if an array contains an object with a certain property value in JavaScript?

If you need to modify the existing Array, you should use splice().

for (var i = array.length - 1; i > -1; i--) {
    if (array[i].name === "zipCode")
        array.splice(i, 1);
}

Notice that I'm looping in reverse. This is in order to deal with the fact that when you do a .splice(i, 1), the array will be reindexed.

If we did a forward loop, we would also need to adjust i whenever we do a .splice() in order to avoid skipping an index.


arr = arr.filter(function (item) {
  return (item.name !== 'zipCode');
});

var i = array.length;
while(i-- > 0) {
    if (array[i].name === "zipCode")
        array.splice(i, 1);
}
  • Loop through the array backwards (so you won't have to skip indexes when splicing)
  • Check each item's name if it's "zipCode"
    • If it is, splice it off using yourArray.splice(index,1);

Then either:

  • continue if there is a possibility of having more than one name having the value "zipCode"
  • break the loop