How to check an Array Contains an Object of Attribute with Given Value in JavaScript ?
Last Updated :
06 Aug, 2024
Sometimes, when working with an array of objects in JavaScript, we need to determine whether an object with a specific attribute value exists in the array. This can be useful when filtering or searching for specific objects in the array. Below are the common methods to determine if the array contains an object with an attribute.
Approach 1: Using Array.some() method
The Javascript array.some() method checks whether at least one of the elements of the array satisfies the condition checked by the argument method. In this approach, we use some() method to find a food item in the food menu array if a searched food item is present in the food menu array it returns true otherwise it returns false.
Syntax
array.some((element, index, array) => {
// Code to test if element matches the condition
});
Example: Here we are using the above-explained approach.
JavaScript
const foodmenu = [
{ Item: "Masala Dosa", price: 60 },
{ Item: "Ice-cream", price: 35 },
{ Item: "Idli", price: 30 },
];
const searchItem = "buttermilk";
const containsObject =
foodmenu.some((obj) => obj.Item === searchItem);
if (containsObject) {
console.log
(`'${searchItem} is present in our Foodmenu'`);
} else {
console.log
(` '${searchItem}' is not present in our Foodmenu`);
};
Output 'buttermilk' is not present in our Foodmenu
Approach 2: Using the Array.find() method
The find() method returns the first element in the array that satisfies the provided testing function. If no element satisfies the function, undefined is returned.
Syntax:
array.find((element, index, array) => {
// Code to test if element matches the condition
});
Example: Here's how we can use find() to determine if an array contains an object with an attribute that equals a given value:
JavaScript
const foodmenu = [
{ Item: "Masala Dosa", Price: 60 },
{ Item: "Ice-cream", Price: 35 },
{ Item: "Idli", Price: 30 },
];
const searchItem = "Idli";
const foundObject =
foodmenu.find((obj) => obj.Item === searchItem);
if (foundObject) {
console.log
(`'${searchItem}' is present in our Foodmmenu`);
} else {
console.log
(`'${searchItem}' is not present in our Foodmenu`);
};
Output'Idli' is present in our Foodmmenu
Approach 3: Using a for loop
for loop is the execution of a set of instructions repeatedly until some condition evaluates and becomes false. in this approach, we are using for loop to determine an object in an array
Syntax:
for (let i = 0; i < array.length; i++) {
if (array[i].attribute === searchValue) {
// Code to handle matching element
}
}
Example: In this example, we are using the above approach.
JavaScript
const foodmenu = [
{ Item: "Masala-Dosa", Price: 60 },
{ Item: "Ice-cream", Price: 35 },
{ Item: "Idli", Price: 30 },
];
const searchItem = "Masala-Dosa";
let found = false;
for (let i = 0; i < foodmenu.length; i++) {
if (foodmenu[i].Item === searchItem) {
found = true;
break;
}
}
if (found) {
console.log
(`'${searchItem}' is present in our Foodmenu`);
} else {
console.log
(`'${searchItem}' is not present in our Foodmenu`);
};
Output'Masala-Dosa' is present in our Foodmenu
Approach 4: Using Array.filter()
Create a new array with all elements passing the test provided by the function via Array.filter(), if you want to know if the filtered array contains any elements, use it.
Syntax:
array.filter((element, index, array) => {
// Code to test if element matches the condition
});
Example: In the given below example we will use array filter() method.
JavaScript
const foodMenu = [
{ name: 'Idli' },
{ name: 'Vada' },
{ name: 'Masala-Dosa' }
];
const searchItem = 'Idli';
const result = foodMenu.filter(item => item.name === searchItem);
if (result.length > 0) {
console.log(`'${searchItem}' is present in our Foodmenu`);
} else {
console.log(`'${searchItem}' is not present in our Foodmenu`);
}
Output'Idli' is present in our Foodmenu
Approach 5: Using Array.includes()
A certain value is included in the entries of the array using Array.includes() method, it returns true if it does, false if it does not, however, this method works mostly with primitive types but for objects it may also be used by comparing references or mapping to a value in order to do it.
Syntax:
array.map(element => element.attribute).includes(searchValue);
Example: In the given below example we will use array includes() method.
JavaScript
const foodMenu = [
{ name: 'Idli' },
{ name: 'Vada' },
{ name: 'Masala-Dosa' }
];
const searchItem = 'Idli';
const result = foodMenu.map(item => item.name).includes(searchItem);
if (result) {
console.log(`'${searchItem}' is present in our Foodmenu`);
} else {
console.log(`'${searchItem}' is not present in our Foodmenu`);
}
Output'Idli' is present in our Foodmenu
Similar Reads
How to check if an Array contains a value or not? There are many ways for checking whether the array contains any specific value or not, one of them is: Examples: Input: arr[] = {10, 30, 15, 17, 39, 13}, key = 17Output: True Input: arr[] = {3, 2, 1, 7, 10, 13}, key = 20Output: False Approach: Using in-built functions: In C language there is no in-b
3 min read
How to check if an array includes an object in JavaScript ? Check if an array includes an object in JavaScript, which refers to determining whether a specific object is present within an array. Since objects are compared by reference, various methods are used to identify if an object with matching properties exists in the array.How to check if an array inclu
3 min read
How to find property values from an array containing multiple objects in JavaScript ? To find property values from an array containing multiple objects in JavaScript, we have multiple approaches. In this article, we will learn how to find property values from an array containing multiple objects in JavaScript. Several methods can be used to find property values from an array containi
4 min read
How to Check a Value Exist at Certain Array Index in JavaScript ? Determining if a value exists at a specific index in an array is a common task in JavaScript. This article explores various methods to achieve this, providing clear examples and explanations to enhance your coding efficiency.Below are the different ways to check if a value exists at a specific index
5 min read
How to Find Property Values in an Array of Object using if/else Condition in JavaScript ? Finding property values in an array of objects using if/else condition is particularly useful when there is a collection of objects. Table of ContentUsing Array Find MethodUsing Array Filter MethodUsing For Of LoopUsing Array Map MethodUsing Array Reduce MethodUsing Array Some MethodUsing Array Find
5 min read
How to check whether an array includes a particular value or not in JavaScript ? In this article, we will discuss the construction of an array followed by checking whether any particular value which is required by the user is included (or present) in the array or not. But let us first see how we could create an array in JavaScript using the following syntax- Syntax: The followin
3 min read
How to Check Object is an Array in JavaScript? There are two different approaches to check an object is an array or not in JavaScript.1. Using Array.isArray() MethodThe Array.isArray() method determines whether the value passed to this function is an array or not. This method returns true if the argument passed is an array else it returns false.
1 min read
How Check if object value exists not add a new object to array using JavaScript ? In this tutorial, we need to check whether an object exists within a JavaScript array of objects and if they are not present then we need to add a new object to the array. We can say, every variable is an object, in Javascript. For example, if we have an array of objects like the following. obj = {
3 min read
How to check whether an object exists in javascript ? Checking whether an object exists in JavaScript refers to determining if a variable or property has been declared and contains a valid value. This helps avoid errors when accessing or manipulating objects that may be undefined, null, or not initialized properly.Here we have some common approaches to
3 min read
How to Check if a Variable is an Array in JavaScript? To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript
2 min read