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.
- Using array.some() method
- Using array.find() method
- Using for loop
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