Categories
JavaScript Answers

How to filter array of objects with Lodash based on property value with JavaScript?

Spread the love

Sometimes, we want to filter array of objects with Lodash based on property value with JavaScript.

In this article, we’ll look at how to filter array of objects with Lodash based on property value with JavaScript.

How to filter array of objects with Lodash based on property value with JavaScript?

To filter array of objects with Lodash based on property value with JavaScript, we use the filter method.

For instance, we write

const myArr = [
  { name: "john", age: 23 },
  { name: "john", age: 43 },
  { name: "jim", age: 101 },
  { name: "bob", age: 67 },
];

const johnArr = _.filter(myArr, (person) => person.name === "john");
console.log(johnArr);

to call filter with myArr with a callback that checks if person.name is 'john' to return an array that has name property set to 'john'.

Conclusion

To filter array of objects with Lodash based on property value with JavaScript, we use the filter method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *