How to use array that include and check an object against a property of an object?


The task is to check whether the array contains a particular value. Also, we need to check if the array contains the particular object with the given property.

This tutorial will use the array.includes(), and array.some() method to check whether the array contains the value or object with a particular property.

Use the array.includes() method to check values exist in the array

The array.includes() method allows us to check whether the array contains any value. In simple terms, we can search for values in the array using the array.includes() method.

Syntax

Users can follow the syntax below to use the array.includes() method to search for a value in the array.

array.includes(value, startIndex);

In the above syntax, the array contains various elements such as strings, numbers, and Booleans.

Parameters

  • Value − It is a value to search in the array.

  • startIndex − It is an optional parameter to start searching from the startIndex.

Return value

It returns the boolean value based on whether or not the value exists in the array.

Example 1

In the example below, we have used the array.includes() method without passing the startIndex, option parameter. So, it will start searching in the array from the 0th index. In the output, users can observe that array.includes() method returns true for the “hello” string values and false for the “abcd” string value.

<html>
<body>
   <h2>Using the <i>array.includes()</i> method to check for the existence of the value in the array.</h2>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      let array = ["Hello", 10, "Hi", true, "Welcome", false, 30, 50, 70, false];
      let result = array.includes("Hello");
      output.innerHTML += "Is Hello exist in the array? " + result + "<br/>";
      result = array.includes("abcd");
      output.innerHTML += "Is abcd exist in the array? " + result + "<br/>";
   </script>
</body>
</html>

In the above approach, we learned to check whether a value exists in the array object. Now, we will learn to check whether an object with particular property exists in the array.

Use the array.some() method to check whether an object with particular property exists in the array

The array.some() method checks if at least one element of the array follows the particular condition passed into the callback function. So, in the callback function, we will check whether any object contains a particular property.

Syntax

Users can follow the syntax below to use the array.some() method to check for the object's existence with the particular property in the array.

let result = objects.some((object) => property in object);

In the above syntax, we have used the ‘in’ operator to check if a property exists in any object of all objects of the array.

Example 2

In the example below, we have created an array of objects, and every object contains various properties and values. Also, we have used the array.some() method and checking that any objects exist in the array containing property passed as a parameter of the checkProperties() function using the ‘in’ operator. Furthermore, we invoke the checkProperties() function with different parameter values on the button click event.

In the output, we get the true if any single object contains the particular property; Otherwise false.

<html>
<body>
   <h2>Using the <i>array.some()</i> method to check for the existence of the object with a particular property.</h2>
   <div id = "output"> </div>
   <button onclick = "checkProperties('salary'); checkProperties('id')">  Check for Object </button>
   <script>
      let output = document.getElementById('output');
      function checkProperties(property) {
         let objects = [
            { prop1: "value1", prop2: "Value2", num: 30 },
            { name: "name", prop3: "Value3", salary: 40860 },
            { age: 20, prop2: "Value2", number: 60 },
            { prop1: "value10", prop2: "Value30", num: 90 },
            { prop1: "value20", prop2: "Value40", num: 100 }
         ];
         let isProperty = objects.some((object) => property in object);
         output.innerHTML += "Is objects array contain any object with " + property + " property? " + isProperty + "<br/>";
      }
   </script>
</body>
</html>

Example 3

In the example below, we have used the array.reduce() method with the array of objects. In the callback function of the reduce() method, we access the salary property of the object and check if it exists in the object by comparing its value with the “undefined” string value.

So, It is another way to find any object containing a particular property using the some() method.

<html>
<body>
   <h2>Using the <i>array.some()</i> method to check for the existence of the object with a particular property.</h2>
   <div id = "output"> </div>
   <button onclick = "checkProperties()"> Check for Object </button>
   <script>
      let output = document.getElementById('output');
      function checkProperties() {
         let objects = [
            { color: "blue", id: "232d", num: 30 },
            { name: "name", prop3: "534", maximum: 10 },
            { age: 20, id: "dred", numValue: 90 },
            { color: "blue", id: "87gd", minimum: 0 },
            { color: "green", id: "56fghfh", num: 100 }
         ];
         let isSalary = objects.some((object) => { object.salary != "undefined" });
         output.innerHTML += "Is objects array contains any object with " + "salary" +    "property? " + isSalary + "<br/>";
      }
   </script>
</body>
</html>

We have used the array.includes() and array.some() method to search a value and object in the array. However, users can also use the filter() method in JavaScript to check if the array contains at least one object with a particular property.

Updated on: 16-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements