Check If Array Contains An Object In JavaScript

You can check if array contains an object in JavaScript by using any one of these methods –

  1. Use the Array.some() method
  2. Use the Array.find() method
  3. Use the Array.findIndex() method
  4. Use the Array.filter() method

Let’s discuss each of these methods in detail.

check if array contains an object in javascript

Use the Array.some() method To Check If Array Contains an Object in JavaScript

The some() method checks whether at least one element in an array passes a condition specified in a callback function. It returns a Boolean value – true if at least one element passes the test and false otherwise.

Consider the following example:

const arr = [{ id: 1 }, { id: 2 }, { id: 3 }];

const containsObj = arr.some(obj => obj.id === 2);

console.log(containsObj); // true

if(containsObj) {

// do something…

}

In the above example, we have an array of objects – arr. We use the some() method to check if this array contains an object with the id property set to 2. The callback function passed to the some() method returns true if the id property of an object is equal to 2. Otherwise, it returns false.

As the some() method only needs one element to return true for the entire condition to be true, it returns true as soon as it finds an object with the id property set to 2.

Use the Array.find() method To Check If Array Contains an Object in JavaScript

The find() method returns the value of the first element in an array that satisfies a condition specified in a callback function. It returns undefined if no element passes the test.

Consider the following example:

const arr = [{ id: 1 }, { id: 2 }, { id: 3 }];

const containsObj = arr.find(obj => obj.id === 2);

console.log(containsObj); // { id: 2 }

if(containsObj !== undefined) {

// do something…

}

In the above example, we have an array of objects – arr. We use the find() method to check if this array contains an object with the id property set to 2. The callback function passed to the find() method returns the first object from the array that has its id property set to 2.

The find() method returns the value of this object. As this object is not undefined, the if condition evaluates to true and we can execute the code inside it.

Use the Array.findIndex() method To Check If Array Contains an Object in JavaScript

The findIndex() method returns the index of the first element in an array that satisfies a condition specified in a callback function. It returns -1 if no element passes the test.

Consider the following example:

const arr = [{ id: 1 }, { id: 2 }, { id: 3 }];

const containsObj = arr.findIndex(obj => obj.id === 2);

console.log(containsObj); // 1

if(containsObj !== -1) {

// do something…

}

In the above example, we have an array of objects – arr. We use the findIndex() method to check if this array contains an object with the id property set to 2. The callback function passed to the findIndex() method returns the index of the first object from the array that has its id property set to 2.

The findIndex() method returns the value of this index. As this index is not -1, the if condition evaluates to true and we can execute the code inside it.

Use the Array.filter() method To Check If Array Contains an Object in JavaScript

The filter() method creates an array of elements that pass a condition specified in a callback function.

Consider the following example:

const arr = [{ id: 1 }, { id: 2 }, { id: 3 }];

const containsObj = arr.filter(obj => obj.id === 2);

console.log(containsObj); // { id: 2 }

if(containsObj.length>0){

  console.log(“Array contains object with id 2”);

} else {

  console.log(“Array doesn’t contain object with id 2”);

}

In the above example, we have an array of objects. We use the filter() method to check if that array contains an object with the id 2. Since there is indeed such an object in the array, the filter() method returns an array containing that object.

We then check the length of that array to see if it is greater than 0 – which it is, meaning that the Array does indeed contain the object we are looking for.

Conclusion

In this article, you have learned several ways to check if an array contains an object in JavaScript. You can use the some(), find(), findIndex() or filter() methods to do so.

If you have any questions or comments, please let us know.

Leave a Comment