0

I have an array of objects and want to remove all the objects that have a specific key value.

the Array looks something like this:

var myArray = [{name: "a", value: "b"}, {name: "a", value: "d"}, {name: "f", value: "r"}, {name: "g", value: "q"}];

In this case, I would like to have all objects with the name a removed form the array.

Help would be appreciated. Thanks!

1 Answers1

1

Array.prototype.filter is what you are looking for, check this out for more information.

const arr = [{name: "a", value: "b"}, {name: "a", value: "d"}, {name: "f", value: "r"}, {name: "g", value: "q"}];

console.log(arr.filter((item) => {
  return item.name !== "a";
}));

Check this out for more information on Array.prototype.filter, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38