While working with an array of objects in JavaScript, it is common to ask, “Does the JavaScript array contain objects?”. Finding this is easy in JavaScript. In this blog, we will discuss the various methods to check if an array includes an object in JavaScript, even when the object structure is the same, but not the exact reference.
Table of Contents:
Why includes() Fails for Objects in JavaScript Arrays
The problem is very simple – you have to find a JavaScript object with some specific properties from an array of objects. Here, the main aspect is to understand the object reference in JavaScript to solve this problem. Let us understand it with the help of an example:
const learner = [
{ id: 121, name: "IntellipaatLearner1" },
{ id: 122, name: "IntellipaatLearner2"
];
You have a learner object. Suppose you have to find the name of the student who has an id equal to 121. Then how would you do this?
Methods to Check if an Array Contains an Object in JS
Well, JavaScript provides you with various methods to check if a specified object is present inside an array or not. Let us discuss all of them one by one:
Method 1: Using includes() for Reference Comparison
The includes() method is used to check objects in an array in JavaScript for both primitive and reference data types. But this method returns true only for an exact object reference in JavaScript. Thus, it is not used widely because it creates confusion among beginners.
Example:
Output:
Explanation: In this example, arr.includes(learner) returns true because you are checking with the same object reference in JavaScript (arr and learner have the same reference). While arr.includes({ id: 1 }) returns false because { id: 1 } is a new object, and the .includes() method returns false if both are not of the same reference type.
Become a Job-Ready Web Developer in Just a Few Months!
Join Our Web Dev Course
Method 2: Using some() for Property-Based Check
The .some() method is used when you want to check whether a certain condition is met or not, for any object inside the array. It basically compares object values in a JavaScript array to find matches based on their properties.
Example:
Output:
Explanation: In this example, you are using the .some() method to check for a specific object inside the array of objects by properties. This method is more flexible than the .includes().
Method 3: Using findIndex() to Find Object Location
If you want to not only check the existence but also want to find the position of the matching object, then the findIndex() method is used. It is another method that compares object values in a JavaScript array.
Example:
Output:
Explanation: In this example, you are using the findIndex() method to find the existence and index of the object. If no object matches, then it returns -1. As in the example, findIndex(item => item.id === 1) basically checks for the object having id: 1, and the object found at index 0.
Method 4: Deep Comparison Using Lodash isEqual()
Lodash is a powerful library in JavaScript that is used for various purposes. It is also good for a clean and readable deep comparison (Comparing nested objects), especially when paired with the .isEqual() method. It checks for deep equality and helps you to compare object values in a JavaScript array, even with nested structures.
Example:
Output:
Explanation: In this example, you are using the Lodash library to check whether a specific deeply nested object is present inside an array of objects or not. If it is present, then you get the object as output otherwise, you will get undefined.
Method 5: Convert to String with JSON.stringify() for Comparison
You can also use JSON.stringify() to convert objects to strings and then perform the searching. It compares objects as a string, providing a way to compare object values in a JavaScript array for simple cases. But it does not work well if:
- The order of keys is different
- It ignores properties with values like undefined, functions, or symbols.
Example:
Output:
Explanation: In this example, you are using JSON.stringify() method. It searches for the target object inside the learner array of objects and returns the output in the form of true or false. If the keys are written in a different order in the target object, then JSON.stringify() fails to produce the correct result.
Quick Comparison of Methods
Here is a comparison between all the methods discussed above:
Method |
What it checks |
Best Used For |
Time Complexity |
includes() |
It looks for the exact same object. |
When you have the same object reference |
O(n) |
some() |
It checks for an object that meets a condition. |
When you want to find the matched objects based on the property value |
O(n) |
findIndex() |
Checks for an object and returns an index value. |
When you also want to find the index or location of the matched object. |
O(n) |
find() + isEqual() |
It checks for deep equality. |
It is used for matching nested objects. |
O(n) + Deep Comparison |
JSON.stringify() + some() |
Compares objects as a string. |
Used for finding a normal object match. |
O(n) + O(k) k = size of object |
LTR Comparison: Overview Table
Method/Approach |
What It Does |
When to Use |
Limitation |
=== (Strict Equality) |
Compares object references, not values |
When you only care if two variables point to the exact same object |
Fails if objects have the same properties but are different references |
JSON.stringify() |
Converts objects to strings and compares |
Quick checks for simple objects with same property order |
Not reliable if property order differs or for objects with methods/undefined |
Array.prototype.some() |
Checks if at least one object in the array matches given condition |
When you only need to know if the object exists |
Doesn’t return the object itself, just a true/false |
Array.prototype.find() |
Finds and returns the first object that matches condition |
When you want the actual object, not just confirmation |
Stops after the first match |
Deep Equality Libraries (like Lodash’s isEqual) |
Compares objects deeply, property by property |
When objects are complex and you want accurate equality |
Extra dependency, slower for very large objects |
In real-world use cases, the best method depends on what you actually need. If you just want to confirm presence, some() is lightweight and effective, while find() is better when you also need the matching object itself; for simple checks, JSON.stringify()
works but has limitations, and for more complex, deeply nested objects, using a library like Lodash’s isEqual
ensures accuracy.
- Native methods are faster – Functions like
some()
, find()
, and includes()
are built into JavaScript and usually perform better than custom-written loops.
- Stringifying objects is costly – Using
JSON.stringify()
for comparison works but can slow things down if the array or objects are very large.
- Deep equality checks are heavier – Libraries like Lodash’s
isEqual()
give accurate results but take more time since they check every property deeply.
- Looping manually can be flexible but slower – Writing custom
for
loops gives full control but can be less efficient than native methods.
- Scale matters – For small arrays, performance differences are negligible, but for very large datasets, picking the right method can have a big impact.
Practical Use Case: Checking Existing Users in a System
Checking an object in an array is important in real-world applications. Let us understand how we can check if a user already exists or not, with the help of an example.
Let’s consider you have a list of users and you want to check if a user with the same email address already exists or not:
This code will help you to find, if the user is already registered with the same email or not. If it is registered, then true is printed on the console as output. Otherwise, false is printed on the console.
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
To check an object in an array, you have to do a comparison by object value or object reference in JavaScript. If you are checking for the same object, then .includes() works. However, other methods like some(), findIndex(), or the Lodash library are more flexible and powerful to check objects in an array of objects.
Learn how to sort an array of objects in JavaScript easily through this blog.
How to check if an array includes an object in JavaScript – FAQs
Q1. How to check an array for a matching value in JavaScript?
If you want to check an array for a matching value in JavaScript, then you can use the includes() method. Here is the syntax for using it:
arr.includes(‘value’);
Q2. How to check if a value exists in an array of objects in JavaScript?
You can use the some() method, if you want to check for the value inside an array of objects in JavaScript.
Q3. How to check if an array has the same value in JavaScript?
The every() method is used to check whether an array has the same value in JavaScript. The every() method iterates over an array and checks whether the elements are equal or not.
Q4. How to check if all values in an array are zero in JavaScript?
You can use the same every() method for checking whether the values inside an array are zero or not.
Q5. How to check if two arrays of objects are equal in JavaScript?
If you want to compare efficiently, then using the Lodash Library is a great choice for you. It is a JavaScript utility library that is used for multiple purposes in JavaScript.
Q6. Does the JavaScript array contain objects?
Yes, a JavaScript array contain objects as its elements.
Q7. Why does array.includes(obj) return false for identical objects?
array.includes(obj) returns false for identical objects because JavaScript compares objects by reference, not by their content.
Q8. How can I check if an array contains an object by its properties?
You can check if an array contains an object by its properties using methods like some() or find() with a property-based comparison.
Q9. Is JSON.stringify() a reliable way to compare objects?
JSON.stringify() can be used to compare objects, but it’s not fully reliable since property order and non-serializable values can cause false differences.
Q10. How do I do deep equality comparison of objects in an array?
You can use libraries like Lodash’s isEqual() or write a recursive function to perform deep equality checks of objects in an array.
Q11. Can I track the index of a matching object?
Yes, you can use methods like findIndex() to track the index of a matching object in an array.
Q12. When should I use find() vs some()?
Use find() when you need the actual matching object, and some() when you only need to know if at least one match exists.