How to Get the Index of an Object in an Array in JavaScript
When working with an array of objects, a common task is to find the index of a specific object based on the value of one of its properties. For example, you might need to find the index of a user with a specific id or a product with a particular sku.
This guide will teach you the modern and standard methods for this task. You will learn how to find the first matching index using findIndex() and how to get an array of all matching indexes using map() and filter().
Goal 1: Get the Index of the First Matching Object
If you only need to find the index of the very first object that satisfies a condition, the Array.prototype.findIndex() method is the perfect tool for the job.
The findIndex() method iterates over an array and returns the index of the first element for which a provided callback function returns a truthy value.
- If a match is found, it returns the index and immediately stops searching.
- If no match is found after checking all elements, it returns
-1.
For example, we have an array of user objects and we want to find the index of the user whose id is 'b'.
// Problem: What is the index of the object with id: 'b'?
const users = [
{ id: 'a', name: 'Alice' },
{ id: 'b', name: 'Bob' },
{ id: 'c', name: 'Charlie' },
];
Recommended Solution:
const users = [
{ id: 'a', name: 'Alice' },
{ id: 'b', name: 'Bob' },
{ id: 'c', name: 'Charlie' },
];
const index = users.findIndex(user => user.id === 'b');
console.log(index); // Output: 1
// Example of a failed search
const notFoundIndex = users.findIndex(user => user.id === 'd');
console.log(notFoundIndex); // Output: -1
This method is the most concise, readable, and efficient way to find the index of the first matching object.
Goal 2: Get the Indexes of All Matching Objects
Sometimes, multiple objects in your array might match your condition, and you need to get the index of all of them.
We have an array where multiple users have the status "active," and we need to find all of their indexes.
// Problem: What are the indexes of all objects with status: 'active'?
const users = [
{ id: 'a', status: 'active' },
{ id: 'b', status: 'inactive' },
{ id: 'c', status: 'active' },
];
Recommended Solution: a clean, functional approach is to chain the map() and filter() methods.
const users = [
{ id: 'a', status: 'active' },
{ id: 'b', status: 'inactive' },
{ id: 'c', status: 'active' },
];
const activeIndexes = users
.map((user, index) => (user.status === 'active' ? index : -1))
.filter(index => index !== -1);
console.log(activeIndexes); // Output: [0, 2]
How it works:
map((user, index) => ...): We firstmapover the array. The callback receives both theuserand itsindex.- If the user's status is "active," we return its
index. - If not, we return a placeholder value like
-1. - The result of this step is
[0, -1, 2].
- If the user's status is "active," we return its
filter(index => index !== -1): We thenfilterthis new array, keeping only the elements that are not-1. This leaves us with a clean array of just the matching indexes.
An Alternative: The reduce() Method
Another powerful functional approach is to use reduce() to build the array of indexes in a single pass.
const users = [
{ id: 'a', status: 'active' },
{ id: 'b', status: 'inactive' },
{ id: 'c', status: 'active' },
];
const activeIndexes = users.reduce((accumulator, user, index) => {
if (user.status === 'active') {
accumulator.push(index);
}
return accumulator;
}, []);
console.log(activeIndexes); // Output: [0, 2]
Conclusion
Finding the index of an object in an array is a common task with clear, modern solutions in JavaScript.
- To find the index of the first matching object, the most direct and efficient method is
array.findIndex(callback). - To get an array of the indexes of all matching objects, a functional chain of
map()followed byfilter()provides a readable and robust solution. Thereduce()method is an excellent and equally valid alternative.