Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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() methods 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
array.includes(value, startIndex);
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
In the example below, we have used the array.includes() method without passing the startIndex optional 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 value 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>
Is Hello exist in the array? true Is abcd exist in the array? false
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
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 1: Using the 'in' Operator
In the example below, we have created an array of objects, and every object contains various properties and values. We use the array.some() method to check if any object exists in the array containing a specific property.
<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>
Is objects array contain any object with salary property? true Is objects array contain any object with id property? false
Example 2: Using Property Access
In the example below, we use the array.some() method by checking if the property value is not undefined. This is another way to find any object containing a particular property.
<html>
<body>
<h2>Using the <i>array.some()</i> method to check property existence by value comparison.</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/>";
let isColor = objects.some((object) => object.color !== undefined);
output.innerHTML += "Is objects array contains any object with color property? " + isColor + "<br/>";
}
</script>
</body>
</html>
Is objects array contains any object with salary property? false Is objects array contains any object with color property? true
Comparison of Methods
| Method | Use Case | Syntax |
|---|---|---|
array.includes() |
Check if array contains a specific value | array.includes(value) |
array.some() with 'in' |
Check if any object has a specific property | array.some(obj => prop in obj) |
array.some() with undefined |
Check property existence by value comparison | array.some(obj => obj.prop !== undefined) |
Conclusion
Use array.includes() for checking primitive values and array.some() for checking object properties. The 'in' operator is preferred for property existence checks as it works even when the property value is undefined.
