Finding Matches in Arrays
indexOf() and includes() work well on arrays of strings and numbers, but they cannot find an object by its contents. For arrays of objects, search with a callback that compares the property that identifies a record, such as an id.
Why indexOf misses an object
Section titled “Why indexOf misses an object”Objects compare by reference. Two separate objects with the same properties are still two different objects, so indexOf() and includes() match only the very object stored in the array:
const users = [ { id: 1, name: 'Ana' }, { id: 2, name: 'Ben' }, { id: 3, name: 'Chloe' },];
const lookalike = { id: 2, name: 'Ben' };console.log(users.indexOf(lookalike));console.log(users.includes(lookalike));
const sameObject = users[1];console.log(users.indexOf(sameObject));This prints:
-1false1Matching on a property
Section titled “Matching on a property”findIndex(), find() and some() each take a callback, so the test can compare whatever identifies a record:
const users = [ { id: 1, name: 'Ana' }, { id: 2, name: 'Ben' }, { id: 3, name: 'Chloe' },];
const target = { id: 2, name: 'Ben' };
const index = users.findIndex((user) => user.id === target.id);console.log(index);
const match = users.find((user) => user.id === target.id);console.log(match);
console.log(users.some((user) => user.id === 9));This prints:
1{ id: 2, name: 'Ben' }falseChoose the method by what you need back:
some()returnstrueorfalse, for a yes-or-no question.find()returns the first matching item, orundefined.findIndex()returns the index of the first match, or -1.filter()returns a new array of every match, which may be empty.
Every match, or the first
Section titled “Every match, or the first”filter() always visits every item, because it is collecting all the matches. find(), findIndex() and some() stop at the first match. Counting the callback calls shows the difference:
const users = [ { id: 1, name: 'Ana', team: 'blue' }, { id: 2, name: 'Ben', team: 'red' }, { id: 3, name: 'Chloe', team: 'blue' }, { id: 4, name: 'Dev', team: 'red' },];
let calls = 0;const reds = users.filter((user) => { calls++; return user.team === 'red';});console.log(reds.map((user) => user.name), calls);
calls = 0;const firstRed = users.find((user) => { calls++; return user.team === 'red';});console.log(firstRed.name, calls);This prints:
[ 'Ben', 'Dev' ] 4Ben 2When only one record can match, as with a unique id, use find() rather than taking filter()[0], because find() says what you mean and stops as soon as it has the answer.
Removing a matched object
Section titled “Removing a matched object”To remove a record from the array itself, find its index and pass it to splice(). Check for -1 first, because splice(-1, 1) removes the last item. To get a new array without the record, use filter() instead:
const users = [ { id: 1, name: 'Ana' }, { id: 2, name: 'Ben' }, { id: 3, name: 'Chloe' },];
const index = users.findIndex((user) => user.id === 2);if (index !== -1) { users.splice(index, 1);}console.log(users);
const remaining = users.filter((user) => user.id !== 1);console.log(remaining);This prints:
[ { id: 1, name: 'Ana' }, { id: 3, name: 'Chloe' } ][ { id: 3, name: 'Chloe' } ]Array filter and Array find and findIndex cover these methods in more detail.
Steve Griffith covers this topic in his video Finding Matches in Arrays.