
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
How do I 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.
Check if array includes object in JavaScript Checking if an Array Includes an Object in JavaScript
In this example, we have used includes() method to check if an array includes an object in JavaScript.
To check existence of the object from starting position 1:
To check existence of the object from starting position 3:
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.
Checking if an Array Includes an Object in JavaScript
In this example, we have used some() method to check if an array includes an object in JavaScript.
Using some() method to check existence of the object in the array
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.
Checking if an Array Includes an Object in JavaScript Checking if an Array Includes an Object in JavaScript
In this example, we have used _.find() method of Lodash library to check if an array includes an object in JavaScript.
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.
Using indexOf() to Check Array Includes Object Check if an Array Includes an Object in JavaScript
In this example, we have used indexOf() to check if an array includes a specific object.
Checking the existence of an object using indexOf():
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.