Categories
JavaScript Answers

How to filter array of objects based on another array in JavaScript?

Spread the love

To filter array of objects based on another array in JavaScript, we call the includes method.

For instance, we write

const filtered = people.filter((person) => idFilter.includes(person.id));

to call people.filter with a callback that calls idFilter.includes to check if person.id is included in the idFilter array.

Then an array with the objects that have id property included in the idFilter array is included.

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 *