How to Find an Element in a JavaScript Array by ID?

Estimated read time 1 min read

You can use the Array.prototype.find method to find an element in a JavaScript array that has a specific property value, such as an id.


const elements = [
    {id: 1, name: "element 1"},
    {id: 2, name: "element 2"},
    {id: 3, name: "element 3"}

const element = elements.find(function(elem) {
    return === 2;


In the example above, the find method returns the first element in the elements array that has an id of 2. The console.log statement logs the following output to the console:

{id: 2, name: "element 2"}

If there are no elements in the array with the specified property value, find will return undefined.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply