Check if Array Contains an Object in JavaScript

avatar

Borislav Hadzhiev

Sun Oct 03 20213 min read

banner

Photo by Rebe Pascual

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 passing it a function
  2. The function should check whether the identifier of the object is equal to a specific value and return true if it is
  3. Array.some will return true is the conditional check is satisfied at least once
index.js
// Supported in IE 9-11 const people = [ {id: 1, name: 'John'}, {id: 2, name: 'Adam'}, ]; const isFound = people.some(element => { if (element.id === 1) { return true; } }); 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 of the array.

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

In our conditional check we verify if the identifier property of the object is equal to a specific value, if it is, then we know that the object exists in the array.

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 passing it a function
  2. The function should check whether the identifier of the object is equal to a specific value and return true if it is
  3. Array.find will return the object if the conditional check is satisfied at least once
index.js
// Not Supported in IE 6-11 const people = [ {id: 1, name: 'John'}, {id: 2, name: 'Adam'}, ]; const person = people.find(element => { if (element.id === 1) { return true; } }); 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, then Array.find will return the array element, otherwise it returns undefined.

In the code example, the condition returns true, therefore 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, however note that `Array.find` is not supported in IE 6-11.

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, passing it a function
  2. The function should check whether the identifier of the object is equal to a specific value and return true if it is
  3. The Array.findIndex method will return the index of the object that satisfies the condition and -1 if none do
index.js
// Not supported in IE 6-11 const people = [ {id: 1, name: 'John'}, {id: 2, name: 'Adam'}, ]; const index = people.findIndex(element => { if (element.id === 1) { return true; } }); 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 returns the index of the element that satisfies the conditional check, 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, passing it a function
  2. The function should check whether the identifier of the object is equal to a specific value and return true if it is
  3. The Array.filter method will return an array containing the objects that satisfy the conditional check (if any)
index.js
// Supported in IE 9-11 const people = [ {id: 1, name: 'John'}, {id: 2, name: 'Adam'}, ]; const filteredArray = people.filter(element => { if (element.id === 1) { return true; } }); 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 would short circuit if their callback function returned a truthy value.

For each element of the array, we verify if its unique identifier matches a specific value, if it does, we know that the object is contained in the array.

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 resulting array.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee