How to Check if an Array Includes an Object in JavaScript?

How to Check if an Array Includes an Object in JavaScript?

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:

Understanding the Object Matching Problem

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 an Array Include an Object

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:

Javascript

Output:

using includes method

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
quiz-icon

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:

Javascript

Output:

using some method

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:

Javascript

Output:

using findIndex method

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:

Javascript

Output:

using lodash library

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:

Javascript

Output:

using JSON stringify method

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.

Method Comparison Table

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

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:

Javascript

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 object in 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 object in array of objects.

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.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner