How to check if an array contains an object in JavaScript?

Last Updated on Mar 31, 2024

a code that checks if an array contains an object in JavaScript

In JavaScript, checking if an array contains a value of a primitive data types such as Number and String is quite different when it comes to non-primitive data types such as objects.

To check if an array contains a value of a primitive data type, you can just use the includes() method in JavaScript.

However, if you use the includes() method to check if an array contains objects, it will return false unless what you supply as a parameter is the object that is inside the array itself. That is because non-primitive data types such as objects are compared by their reference instead of their value.

let obj = {'property': 'value'};
let obj1 = {'property': 'value'}; 
let array = ['fruits', 'vegetables', 'meat', obj];
console.log(array.includes('vegetables')); //true
console.log(array.includes(obj)); //true
console.log(array.includes(obj1)); //false

As you can see from the code above, the last statement returns false, although obj and obj1 have the same values.

In the following sections, I will show you what you need to do to check if an array contains an object in JavaScript.

  1. Compare two objects by value in JavaScript

    First, let's learn how to test two objects for equality by their values.

    As I said earlier, objects are compared by their references, so == or === will not work for testing for equality. See the example below.

    let obj = {'property': 'value'};
    let obj1 = {'property': 'value'}; 
    console.log(obj == obj1); //false
    console.log(obj === obj1); //false
    

    So what we will do instead is convert the objects first into JSON strings before comparing. We can accomplish that using the JSON.stringify() method in JavaScript.

    let obj = {'property': 'value'};
    let obj1 = {'property': 'value'}; 
    console.log(JSON.stringify(obj) == JSON.stringify(obj1)); //true
    

    As you can see from the code above, the comparison returns true because it was first converted to a string before we tested for equality.

  2. Using Loop in JavaScript

    Now that we know how to test objects for equality, we can now check if an array contains an object using a basic loop first before doing it with a built-in loop method in JavaScript.

    let obj = {'property': 'value'};
    let obj1 = {'property': 'value'}; 
    let array = ['fruits', 'vegetables', 'meat', obj];
    for(let counter = 0; counter < array.length; counter++) {
      if (JSON.stringify(array[counter]) == JSON.stringify(obj1)) {
        console.log('It contains the object');
        break;
      }
    }
    

    The code above loops the array and checks each value to see if it's equal to the object, comparing by value using the JSON.stringify() method.

    The way we do it above is working and perfectly fine; however, there is a way where we don't need to create a loop. I'll show this in the next section.

  3. Using some() method in JavaScript

    The some() method is a built-in loop method in JavaScript. It accepts a callback function as a parameter, which tests each element or value in the array. It returns true if at least one element passes the test given by the callback function. Otherwise, it returns false.

    You can access each element of an array using the first parameter of the callback function.

    let obj = {'property': 'value'};
    let obj1 = {'property': 'value'}; 
    let array = ['fruits', 'vegetables', 'meat', obj];
    if(array.some(value => {return JSON.stringify(obj1) == JSON.stringify(value)})) {
      console.log('It contains the object');
    }
    

    As you can see from the code above, by using the some() method in JavaScript, we didn't have to create our own loop to loop through an array.

Summary

In this tutorial, I showed you how to check if an array contains an object using the JSON.stringify() method to compare objects. Then, by using the some() method, we can test each element in the array without creating our own loop.

© John Michael Balbarona