
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if an Array Includes an Object in JavaScript
To check if an array includes an object in JavaScript, is useful while managing a collection of data to verify if any specific object is present. We will be discussing four different approaches with examples to check if an array includes an object in JavaScript.
In this article, we are having an array and an object. Our task is to check if an array includes an object in JavaScript.
Approaches to check if array includes an object
Here is a list of approaches to check if an array includes an object in JavaScript which we will be discussing in this article with stepwise explaination and complete example codes.
Using includes() method
To check if an array includes an object in JavaScript we have used includes() method. It searches for the part of the string and returns boolean value, either true or false.
- We have created an array and an object respectively, and then inserted the object in the array.
- Then we have used array.includes(object, 1) and array.includes(object, 1) to check if array includes the object.
- The parameter object represents the object which we are searching in array and (1&3) represents the position from where we want to search object in array.
- Then we have used div, getElementById method and innerHTML property to display the output.
Example
Here is a complete example code implementing above mentioned steps to check if an array includes an object in JavaScript using includes() method.
<!DOCTYPE html> <html lang="en"> <head> <title> Check if array includes object in JavaScript </title> </head> <body> <h2> Checking if an Array Includes an Object in JavaScript </h2> <p> In this example, we have used <strong>includes()</strong> method to check if an array includes an object in JavaScript. </p> <hr> <p> To check existence of the object from starting position 1: </p> <div id="output1"></div> <p> To check existence of the object from starting position 3: </p> <div id="output2"></div> <script> let output1 = document.getElementById("output1"); let output2 = document.getElementById("output2"); let object = { "name": "TutorialPoints", "website": "TutorialsPoint.com" }; let array = ["TutorialsPoint", "Hello", object, "World"]; let result = array.includes(object, 1); output1.innerHTML = result; result = array.includes(object, 3); output2.innerHTML = result; </script> </body> </html>
Using some() method
In this approach to check if an array includes an object in JavaScript, we have used some() method. It takes a callback function and verifies whether at least one element in the array passes the test provided by the callback function.
- We have created an array and an object respectively, and then inserted the object in the array.
- Then we have used some() method to check if our array includes an object using callback function myfunction.
- We have used typeOf operator to check if item is an object and the result is stored in variable boolean.
- Then we have used div, getElementById() method and innerHTML property to display the output which is a boolean value.
Example
Here is a complete example code implementing above mentioned steps to check if an array includes an object in JavaScript using some() method.
<!DOCTYPE html> <html lang="en"> <body> <h2> Checking if an Array Includes an Object in JavaScript </h2> <p> In this example, we have used <strong>some()</strong> method to check if an array includes an object in JavaScript. </p> <hr> <p> Using some() method to check existence of the object in the array </p> <div id="output1"></div> <script> let output1 = document.getElementById("output1"); let obj = { "name": "TutorialPoints", "website": "TutorialsPoint.com" }; let arr = ["TutorialsPoint", "Hello", obj, "World"]; let result = arr.some(function myfunction(item) { let boolean = typeof item == "object"; return boolean; }); output1.innerHTML = '[ ' +arr +' ]' +': ' +result; </script> </body> </html>
Using Lodash _.find() method
In this approach to check if an array includes an object in JavaScript, we have used Lodash _.find() method.
- We have created an array and an object respectively, and then inserted the object in the array.
- Then we have used _.find(arr, obj) which searches for the obj in the array arr.
- Then we have used JSON.stringify() method to convert the object to string to display the result in our HTML document using div, getElementById() method and innerHTML property.
Example
Here is a complete example code implementing above mentioned steps to check if an array includes an object in JavaScript using Lodash _.find() method.
<!DOCTYPE html> <html lang="en"> <head> <title> Checking if an Array Includes an Object in JavaScript </title> <script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script> </head> <body> <h2> Checking if an Array Includes an Object in JavaScript </h2> <p> In this example, we have used <strong>_.find()</strong> method of <strong>Lodash</strong> library to check if an array includes an object in JavaScript. </p> <hr> <div id="output"></div> <script> let obj = { "name": "TutorialPoints", "website": "TutorialsPoint.com" }; let arr = ["TutorialsPoint", "Hello", obj, "World"]; let res= _.find(arr, obj); document.getElementById("output").innerHTML= "The object present in array: " +JSON.stringify(res); </script> </body> </html>
Using indexOf() method
In this approach we have used indexOf() method to check if an array includes an object in JavaScript. It finds the index of the first occurrence of the specified substring in the original string.
- We have created an array and an object respectively, and then inserted the object in the array.
- Then we have used arr.indexOf(obj); method to find first occurence of obj in array arr and return the index of obj if found. This index is stored in index.
- Then we have used ternary operator which checks the value of index. If given condition is true i.e index !== -1 then it displays the index and if index not found it returns not found.
- Then we have used div, getElementById() method and innerHTML property to display the output.
Example
Here is a complete example code implementing above mentioned steps to check if an array includes an object in JavaScript using indexOf() method.
<!DOCTYPE html> <html lang="en"> <head> <title> Using indexOf() to Check Array Includes Object </title> </head> <body> <h2> Check if an Array Includes an Object in JavaScript </h2> <p> In this example, we have used <strong>indexOf()</strong> to check if an array includes a specific object. </p> <hr> <p> Checking the existence of an object using <strong> indexOf()</strong>: </p> <div id="output"></div> <script> let obj = { name: "TutorialPoints", website: "TutorialsPoint.com" }; let arr = ["TutorialsPoint", "Hello", obj, "World"]; let index = arr.indexOf(obj); let res = index !== -1 ? `Object found at index: ${index}` : "Object not found in the array."; document.getElementById("output").innerHTML = res; </script> </body> </html>
Conclusion
In this article we discussed various approaches to check if an array includes an object in JavaScript, which are: by using includes() method, some() method, Lodash _.find() method (find() method can also be used) and indexOf() method.