This post will discuss how to find a value in an array of objects in JavaScript.

1. Using Array.prototype.find() function

The recommended solution is to use the find() method that returns the first occurrence of an element in the array that satisfies the given predicate. The following code example demonstrates this by finding a person with the name John.

Download  Run Code

2. Using Array.prototype.findIndex() function

Alternatively, you can use the findIndex() method, which is similar to the find() method but returns the index of the first occurrence of an element or -1 if no element is found.

Download  Run Code

3. Using Array.prototype.forEach() function

Here, the idea is to iterate over the given array using the forEach() method and determine whether the object is present in the array.

Download  Run Code

4. Using Array.prototype.filter() function

Another plausible way is to filter the array to return all objects that pass the specified predicate. This can be easily done using the filter() method.

Download  Run Code

5. Using jQuery

The jQuery’s $.grep method works similarly to JavaScript’s native filter() method.

Download Code

6. Using Lodash/Underscore Library

The Underscore and Lodash library have the _.filter method, similar to the JavaScript’s native filter() method. The following code example demonstrates the usage of the _.filter method.

Download Code

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