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 this array contain this object?”. Finding this is easy in JavaScript. In this blog, we will discuss the various methods to check whether an object exists in an array or not, even when the object structure is the same, but not the exact reference.

Table of Contents:

The Problem

The problem is very simple – you have to find a JavaScript object with some specific properties from an array of objects. 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()

The includes() method in JavaScript can be used to check for both primitive and reference data types, but it only returns true for exact reference types like objects and arrays. 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 (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()

The .some() method is used when you want to check whether a certain condition is met or not, for any object inside the array.

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()

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.

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: Using Lodash Library

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.

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: Using JSON.stringify()

You can also use JSON.stringify() to convert objects to strings and then perform the searching. 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.

Comparison Between All 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

Real-Life Example

Checking for a specific object inside an array of objects 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

Checking if an object exists in a JavaScript array or not depends on how you want to compare – by reference or by value. 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 for checking an object inside an 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.

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