Sometimes, we want to find a value in an array of objects in JavaScript.
In this article, we’ll look at how to find a value in an array of objects in JavaScript.
How to find a value in an array of objects in JavaScript?
To find a value in an array of objects in JavaScript, we use the filter method.
For instance, we write
const people = [
{ name: "bob", dinner: "pizza" },
{ name: "john", dinner: "sushi" },
{ name: "larry", dinner: "hummus" },
];
const filterted = people.filter((person) => {
return person.dinner === "sushi";
});
to call people.filter with a callback that checks if the items in people has dinner property equal to 'sushi' and return them in a new array.
Conclusion
To find a value in an array of objects in JavaScript, we use the filter method.