Categories
JavaScript Answers

How to filter array of objects with JavaScript?

Spread the love

Sometimes, we want to filter array of objects with JavaScript.

In this article, we’ll look at how to filter array of objects with JavaScript.

How to filter array of objects with JavaScript?

To filter array of objects with JavaScript, we can use the array filter method.

For instance, we write

const names = [
  { name: "Joe", age: 20, email: "joe@hotmail.com" },
  { name: "Mike", age: 50, email: "mike@hotmail.com" },
  { name: "Joe", age: 45, email: "mike@hotmail.com" },
];
const foundNames = names.filter((v) => v.name === "Joe" && v.age < 30);

to call names.filter with a function at checks if the name of the object v is 'Joe' and the age property is less than 30.

If both conditions are met, then the object will be included in the returned array.

Conclusion

To filter array of objects with JavaScript, we can use the array filter method.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.