Javascript array contains object: In this article, we are going to check if the javascript array contains an object or not.
Checking if Array Contains an Object in JavaScript
We can check whether the array contains an object in javascript in a number of ways. The following are the ways to check:
- Using EmailFilter
- Using Array.find() Method
- Using Array.findIndex() Method
- Using Array.filter() Method
Method #1: Using Array.some() Method
- Invoke/call the Array.some() method by passing it a function.
- The function should verify whether the object’s identifier is equal to a specific value and return true if it is.
- If the conditional check is satisfied at least once, Array.some() method will return true.
Example1:
// This supports in IE 9-11 // Create an array of objects and store it in a variable const websites = [ {sno: 1, websiteName: 'Btechgeeks'}, {sno: 2, websiteName: 'Python-Programs'}, {sno: 3, websiteName: 'SheetsTips'}, ]; // Check whether the array contains any specific object using the // Array.some() function const objectFound = websites.some(element => { // If the object is found return true if (element.sno === 2) { return true; } // Else return false return false; }); console.log(objectFound); if (objectFound) { // object is contained in array }
Output:
true
Example2:
// This supports in IE 9-11 // Create an array of objects and store it in a variable const websites = [ {sno: 1, websiteName: 'Btechgeeks'}, {sno: 2, websiteName: 'Python-Programs'}, {sno: 3, websiteName: 'SheetsTips'}, ]; // Check whether the array contains any specific object using the // Array.some() function const objectFound = websites.some(element => { // If the object is found return true if (element.sno === 4) { return true; } // Else return false return false; }); console.log(objectFound); if (objectFound) { // object is contained in array }
Output:
false
Explanation:
Here sno 4 does not exist in a given object, hence it returns false
- The function that we passed to the Array.some() method will be invoked with each value of the array.
- The Array.some short circuits and returns true, If it returns a truthy value at least once.
- In our condition, we check to see if the object’s identifier field equals a specified value. If it is, we know the object is there in the array.
Method #2: Using Array.find() Method
To check if a JavaScript array contains an object, use the following steps:
- Invoke/call the Array.find() method by passing it a function.
- The function should verify whether the object’s identifier is equal to a specific value and return true if it is.
- If the conditional check is satisfied at least once, Array.find() will return the object.
Example1:
// This does NOT supports in IE 6-11 // Create an array of objects and store it in a variable const websites = [ {sno: 1, websiteName: 'Btechgeeks'}, {sno: 2, websiteName: 'Python-Programs'}, {sno: 3, websiteName: 'SheetsTips'}, ]; // Check whether the array contains any specific object using the // Array.find() function const objectFound = websites.find(element => { // If the object is found return true if (element.sno === 3) { return true; } // Else return false return false; }); console.log(objectFound); if (websites !== undefined) { // Then object is contained in an array }
Output:
{ sno: 3, websiteName: 'SheetsTips' }
Explanation:
Here sno: 3 exists in the given array, hence the Array.find() method returns that object
- The function that we passed to the Array.find() method gets invoked with each array element until it gives a true value or iterates over all array elements.
- If the condition is met, Array.find returns the array element; else, undefined is returned.
- Since the condition in the above example returns true, the find() method returns the object.
NOTE:
When you need to access additional properties on the object, use 'Array.find' instead of 'Array.some'; unfortunately, 'Array.find' is not supported in IE 6-11.
Method #3: Using Array.findIndex() Method
To check if a JavaScript array contains an object, use the following steps:
- Invoke/call the
Array.findIndex()
method by passing it a function. - The function should verify whether the object’s identifier is equal to a given value and return true if it is.
- Array.findIndex returns the index of the object if meets the condition and -1 if none do.
Example1:
// This does NOT supports in IE 6-11 // Create an array of objects and store it in a variable const websites = [ {sno: 1, websiteName: 'Btechgeeks'}, {sno: 2, websiteName: 'Python-Programs'}, {sno: 3, websiteName: 'SheetsTips'}, ]; // Check whether the array contains any specific object using the // Array.findIndex() function const objectFound_index = websites.findIndex(element => { // If the object is found i.e, true then it returns index value if (element.sno === 1) { return true; } // Else if false return -1 return false; }); console.log(objectFound_index); if (objectFound_index !== -1) { // Then object is contained in an array }
Output:
0
Explanation:
Here sno: 1 exists in the given array, hence the findIndex() method returns the index of sno1 i.e 0
Example2:
// This does NOT supports in IE 6-11 // Create an array of objects and store it in a variable const websites = [ {sno: 1, websiteName: 'Btechgeeks'}, {sno: 2, websiteName: 'Python-Programs'}, {sno: 3, websiteName: 'SheetsTips'}, ]; // Check whether the array contains any specific object using the // Array.findIndex() function const objectFound_index = websites.findIndex(element => { // If the object is found i.e, true then it returns index value if (element.sno === 5) { return true; } // Else if false return -1 return false; }); console.log(objectFound_index); if (objectFound_index !== -1) { // Then object is contained in an array }
Output:
-1
Explanation:
Here sno: 5 does NOT exist in the given array, hence the findIndex() method returns -1
The Array.findIndex method is identical to the Array.find method, except that it returns the index of the element that meets the conditional check rather than the element itself.
Array.findIndex executes its callback function with each element in the array until a true value is returned or the array’s values are exhausted.
NOTE:
This Array.findIndex() method returns -1 if all calls to its callback function return a false value.
Method #4: Using Array.filter() Method
To check if a JavaScript array contains an object, use the following steps:
- Invoke/call the Array.filter() method by passing it a function.
- The function should verify whether the object’s identifier is equal to a given value and return true if it is.
- The Array.filter method will return an array of objects that satisfy the conditional check (if any)
Example1
// This supports in IE 9-11 // Create an array of objects and store it in a variable const websites = [ {sno: 1, websiteName: 'Btechgeeks'}, {sno: 2, websiteName: 'Python-Programs'}, {sno: 3, websiteName: 'SheetsTips'}, ]; // Check whether the array contains a specific object using //the Array.findIndex() function const objectFound = websites.filter(element => { // If the object is found i.e, true then it returns that corresponding object values if (element.sno === 2) { return true; } // Else if false returns an empty array return false; }); console.log(objectFound); if (objectFound.length > 0) { // The object is contained in a given array }
Output:
[ { sno: 2, websiteName: 'Python-Programs' } ]
Example2
// This supports in IE 9-11 // Create an array of objects and store it in a variable const websites = [ {sno: 1, websiteName: 'Btechgeeks'}, {sno: 2, websiteName: 'Python-Programs'}, {sno: 3, websiteName: 'SheetsTips'}, ]; // Check whether the array contains a specific object using //the Array.findIndex() function const objectFound = websites.filter(element => { // If the object is found i.e, true then it returns that corresponding object values if (element.sno === 5) { return true; } // Else if false returns an empty array return false; }); console.log(objectFound); if (objectFound.length > 0) { // The object is contained in a given array }
Output:
[]