Check if Array Contains an Object in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Check if an Array contains an Object with Array.some #

To check if a JavaScript array contains an object:

  1. Call the Array.some() method on the array.
  2. Check if each object in the array contains a property with the specific value.
  3. Array.some will return true if the object is contained in the array.
index.js
const people = [ {id: 1, name: 'John'}, {id: 2, name: 'Adam'}, ]; const isFound = people.some(element => { if (element.id === 1) { return true; } return false; }); console.log(isFound); // 👉️ true if (isFound) { // object is contained in array }

The function we passed to the Array.some method will get called with each value (object) in the array.

If it returns a truthy value at least once, the Array.some method short circuits and returns true.

Each time the callback function is called with an object, we check if the object's id property has a value of 1 and return true if it does.

Check if an Array Contains an Object with Array.find #

To check if a JavaScript array contains an object:

  1. Call the Array.find method on the array.
  2. Check if each object in the array contains a property with the specific value.
  3. Array.find will return the object if the object is contained in the array.
index.js
const people = [ {id: 1, name: 'John'}, {id: 2, name: 'Adam'}, ]; const person = people.find(element => { if (element.id === 1) { return true; } return false; }); console.log(person); // 👉️ { id: 1, name: 'John' } if (person !== undefined) { // 👉️ object is contained in array }

The function we passed to the Array.find method gets called with each array element, until it returns a truthy value or iterates over all array elements.

If the condition is satisfied, Array.find returns the corresponding array element, otherwise it returns undefined.

There is an object with an id property set to 1, so the find() method returns the object.

You should use the Array.find method instead of Array.some, when you need to access additional properties on the object.

Check if an Array Contains an Object with Array.findIndex #

To check if a JavaScript array contains an object:

  1. Call the Array.findIndex method on the array.
  2. Check if each object in the array contains a property with the specific value.
  3. The Array.findIndex method will return the index of the object in the array, or -1 if the object is not in the array.
index.js
const people = [ {id: 1, name: 'John'}, {id: 2, name: 'Adam'}, ]; const index = people.findIndex(element => { if (element.id === 1) { return true; } return false; }); console.log(index); // 👉️ 0 if (index !== -1) { // 👉️ object is contained in array }

The Array.findIndex method is very similar to the Array.find method, but it returns the index of the element that satisfies the condition, and not the element itself.

The Array.findIndex method calls its callback function with each element in the array until a truthy value is returned or the values in the array are exhausted.

The Array.findIndex method returns -1 if all calls of its callback function return a falsy value.

Check if an Array Contains an Object with Array.filter #

To check if a JavaScript array contains an object:

  1. Call the Array.filter method on the array.
  2. Check if each object in the array contains a property with the specific value.
  3. The Array.filter method will return an array containing the objects that satisfy the condition (if any).
index.js
const people = [ {id: 1, name: 'John'}, {id: 2, name: 'Adam'}, ]; const filteredArray = people.filter(element => { if (element.id === 1) { return true; } return false; }); console.log(filteredArray); // 👉️ [ { id: 1, name: 'John' } ] if (filteredArray.length > 0) { // 👉️ object is contained in the array }

The Array.filter method gets invoked with every element in the array, which is different than the other methods, which short circuit if their callback function returns a truthy value.

We check if each element (object) in the array contains an id property with a value of 1. If the new array is not empty, then there are matching objects.

Note that the Array.filter method returns an array with all of the elements that satisfy the condition. If we had multiple objects with id equal to 1, all of them would be included in the new array.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.