How do I check if an array includes an object in JavaScript?


In this tutorial, we will learn to check whether an array includes an object or not. The actual array contains the values with the same data types. In JavaScript, the array is replaced with the list, including values with any data type even if users can add the objects inside the list with numbers and strings.

So, programmers sometimes need to figure out if the array contains the objects or not. Programmers can access the object properties and perform some operations if the array contains an object.

Here, we have different methods to check whether the array contains an object.

  • Using the array.includes() method

  • Using the array.some() method

Using the array.includes() method

Array.includes() method is most helpful when we need to check that the array includes the particular value in the list or not in JavaScript. We can find any particular object entity using this method. We will pass an object as a parameter of the array.includes() method, and if the array contains the object with the same key and values, it returns true.

Syntax

Users can follow the below syntax to use the array.includes() method.

let array = [ "TutorialsPoint", "Hello", object, "World" ];
let result = array.includes( object , starting_position );

Parameters

The array.includes() method contains the two parameters.

  • object − It is an entity type of the object which you want to find in the array.

  • starting_position − It is the 0 index-based starting position in array, from which position the user want to start finding the object in the array.

Example

In the below example, we have created a single object and array of strings. Also, it included objects inside the array. We are finding the object in the array by starting various positions using the array.includes() method.

<html>
<head>
   <title>Check if array includes object in JavaScript.</title>
</head>
<body>
   <h3>Check if array includes object in JavaScript.</h3
   <p>Using Array.includes() method to check existence of the object from starting position 1:</p>
   <div id="existance1"></div>
   <p>Using Array.includes() method to check existence of the object from starting position 3:</p>
   <div id="existance2"></div>
   <script>
      let existance1 = document.getElementById("existance1");
      let existance2 = document.getElementById("existance2");
      let object = { "name": "TutorialPoints", "website": "TutorialsPoint.com" };
      let array = [ "TutorialsPoint", "Hello", object, "World" ];
      let result = array.includes(object, 1); // we are checking from the starting position 1 instead of zero.
      existance1.innerHTML = result;
      result = array.includes(object, 3);
      existance2.innerHTML = result;
   </script>
</body>
</html>


In the above example code, the object's position is 2 as the list starts from the index 0. If we start to find the object position from 1, we will catch the object and returns true. When we start to find the object from the 3rd position, it returns false.

Using the array.some() method

The array.some() method iterates through every element of the array just like forEach loop in JavaScript. It takes the callback function as a parameter. Uses can define some conditions to apply to every array element inside the callback function. Even if the condition becomes true for the single element, it returns true. In our case, we will check the type of value, and if it is an object, it will return true.

Syntax

The syntax for the array.some() method is as given below.

let array = [ "TutorialsPoint", "Hello", object, "World" ]
let result = array.some( function callback(item) {
   // condition of the callback function.
   return typeof item == "object";
});

Also, users can use the arrow function inside the callback function. Here, we have created the named function to maintain the simplicity of the code.

Parameters

The array.some() method takes the callback function as a parameter.

  • callback function − It is a function that checks for some condition on every element. If any single element evaluates the condition true, array.some() method returns true.

Example

The below example demonstrates that array.some() method. We have created the array which contains the object. We will iterate through every element of the array to check its type. If an array contains any single entity, which type is an object, array.some() returns true.

<html>
<body>
   <h3>Check if array includes object in JavaScript.</h3>
   <p>Using some() method to check existence of the object in the array:</p>
   <div id="existance1"></div>
   <script>
      let existance1 = document.getElementById("existance1");
      let existance2 = document.getElementById("existance2");
      let obj = { "name": "TutorialPoints", "website": "TutorialsPoint.com" };
      let arr = [ "TutorialsPoint", "Hello", obj, "World" ];
      let result = arr.some( function myfunction(item) {
         // checking the type of every item and if it is object it returns true.
         let boolean = typeof item == "object";
         return boolean;
      });
      existance1.innerHTML = result;
   </script>
</body>
</html>


Conclusion

In this tutorial, we have learned two methods to check the array, including the object. The first method checks for the object with the same key-value pair exist in the array or not. Rather than checking for the object with the same key-value pair, the second method checks for the entity of the object type.

However, there are many other methods in JavaScript to check the existence of the object entity in the array. Users can use the find() method, which works similarly to the array.some() method. Furthermore, array,filter(), and array.indexof() method is also useful to solve problems given in this tutorial.

Updated on: 20-Jul-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements