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.

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.