JavaScript

Find a Value in an Array of Objects in JavaScript

Sometimes, developers need to search for a specific piece of data in an array of objects based on a certain value or property, update or delete any data from it, and so on. To find the specific value in an array of objects, use the JavaScript built-in methods called the “find()” method or the “findIndex()” method to get the index of the specific value.

This article will demonstrate the procedure to find/determine the value in an array of objects in JavaScript.

How to Find/Determine a Value in an Array of Objects in JavaScript?

For finding a value/element in an array of objects, utilize the following methods:

Method 1: Find/Determine a Value in an Array of Objects Using “Array.find()” Method

To find/determine the value/element in an array of objects, use the “Array.find()” method. The find() method gives the 1st element in the array that satisfies/fulfill the specified function.

Syntax

Use the given syntax for the find() method:

find((element) => {

//

})

This method accepts a callback function as a parameter, which is called for every element in the array. This callback function should give a boolean value that indicates whether the current element satisfies the condition.

Example

Create an array of objects called “userInfo” that stores the user’s information as an object:

var userInfo = [
{
 name: "Mari",
 age: 25,
 designation: 'HR'
},
{
 firstName: "Emma",
 age: 20,
 designation: 'Accounts'
},
{
 firstName: "Mia",
 age: 28,
 designation: 'Admin'
}
];

Call the “find()” method to get the user object whose designation is “HR”:

var valueFound = userInfo.find(obj => obj.designation === 'HR');

Print the result on the console:

console.log(valueFound);

It’s worth noting that the find() method only returns the first element that satisfies the single or multiple conditions:

It can be observed that the find() method successfully searches through the array and find the user object that has the matching designation.

Method 2: Find/Determine a Value in an Array of Objects Using “Array.findIndex()” Method

Use the “findIndex()” method, which also allows finding the value in an array of objects. Unlike the find() method, findIndex() outputs the index of the first element that fulfills a specified condition rather than the element itself. If no element is found/exists, it returns -1.

Syntax

Follow the given syntax for the findIndex() method:

findIndex((element) => {

//

})

This method also accepts a callback function as a parameter.

Example

Call the findIndex() method to find the user object from the “userInfo” array whose designation is “Accounts”. It will return the index of the first element whose designation is equivalent to the Accounts:

var valueIndex = userInfo.findIndex(obj => obj.designation === 'Accounts');

Check the returned index, if it is not equal to -1, then print the value of that index on the console:

if (valueIndex !== -1) {

console.log(userInfo[valueIndex]);

}

Output

Method 3: Find/Determine a Value in an Array of Objects Using “for” Loop

The most commonly used way to get any value from an array, object or an array of objects, use the “for” loop. It iterates through the array and, within the loop, utilizes an if statement to verify if the current element fulfills the condition. Once the element is found, stop the loop using the “break” statement.

Syntax

For using the “for” loop, utilize the provided syntax:

for(var i=0; i< array.length; i++) {

//.....

}

Example

First, set the value -1 to the variable “valueFound”, which will be updated to the index that satisfies the condition:

var valueFound = -1;

Iterate the array until its length and identify the object whose age is equal to “28”. If the object is found, update the index of the variable “valueFound” and stop the loop:

for(var i=0; i< userInfo.length; i++) {

if(userInfo[i].age == '28') {

valueFound = i;

break;

}

}

Finally, print the object on the console using the “console.log()” method:

console.log(userInfo[valueFound]);

As you can see that the object whose age is 28 has been printed on the console:

That’s all about finding the value in an array of objects in JavaScript.

Conclusion

To find the value/element in an array of objects, use the “Array.find()” method, “Array.findIndex()” method, or the “for” loop. The find() method only returns the first element that satisfies the condition, while the findIndex() method gives the index of the first elements that fulfill the specified condition. This article demonstrated the procedure for finding the value in a JavaScript array of objects.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.