HOWTO · JavaScript

How to Find Index of Object in JavaScript Array

Find the index of the first object in a JavaScript array that matches a property condition.

On this page

Use the native findIndex() method to locate the position of the first object that meets a condition. For example, a predicate can compare an object’s name, id, or role property. findIndex() returns a number: the matching object’s array index, or -1 when no object matches. It does not return the object itself.

Use findIndex() to Find an Object’s Index

Call findIndex() on the array and pass it a callback that returns a truthy value for a match. This example searches an array of people for the first editor and records which positions the method checks:

Run this example:

const people = [
  { name: "Mina", role: "author" },
  { name: "Luis", role: "editor" },
  { name: "Ava", role: "editor" },
];

const checkedIndexes = [];
const index = people.findIndex((person, currentIndex) => {
  checkedIndexes.push(currentIndex);
  return person.role === "editor";
});

console.log(index);
console.log(checkedIndexes.join(", "));
1
0, 1

The returned value is 1 because Luis is the first editor. Ava also matches, but the search stops as soon as it finds Luis, so index 2 is not checked. The same method works with primitive arrays: for example, [3, 10, 17, 23].findIndex(age => age > 18) returns the position of the first age above 18, which is 3.

Understand the findIndex() Callback and Return Value

The callback receives the current array value first. It may also accept the current index and the array being searched, in that order: (currentValue, index, array). The example uses the index to show the search order; use the array argument when a predicate needs context from the original collection. Usually the current value is enough for an object-property lookup.

The callback must test the property you care about. A predicate such as person.role === "editor" returns a Boolean, while a broader condition can match several objects; findIndex() still reports only the first match. If every callback result is false, the method returns -1. Check for that result before using the value as an array position, since people[-1] is not the final item and will not retrieve a match.

For a direct lookup, compare a stable property such as an identifier: people.findIndex(person => person.id === targetId). Use strict equality when the expected value has the same type as the stored property. If the task is to retrieve the matching object rather than its position, use find() instead; the two methods share predicate-style searching but return different kinds of values.

Use Lodash _.findIndex() as an Alternative

If a project already uses lodash, its _.findIndex() function offers a similar lookup without calling an array method. Add the library through the project’s package manager (for example, npm i lodash or yarn add lodash). The Lodash _.findIndex() documentation describes its predicate shorthands and options.

Pass the array first and a predicate second. For the people array above, _.findIndex(people, { role: "editor" }) returns 1; Lodash treats the object predicate as a property match. You can instead pass a function such as person => person.role === "editor" when the condition needs custom logic. The optional fromIndex argument sets where searching begins: _.findIndex(people, { role: "editor" }, 2) starts at index 2, so it finds Ava there. This differs in form from native people.findIndex(predicate), but both return the first matching index from their respective starting positions, or -1 if no later item matches. Choose lodash when it is already part of the project or its predicate shorthand is useful; the native method needs no additional library.