How to check if an array includes an object in JavaScript ?

In JavaScript, an array acts like like list. It’s a way to group multiple items together under one name. You can store different things in your list, like numbers, words, or even other lists. Each item in the list has its own place, and you can access it using a number called its “index.”

We will check if an array includes an object or not in JavaScript. There are various methods to check whether an array includes an object or not.

How to check if an array includes an object in JavaScript ?


These are the following approaches by using these we can check if an array includes an object in JavaScript:

Table of Content

  • Using includes() Method
  • Using some() Method
  • Using filter() Method
  • Using findIndex() Method
  • Using Lodash _.find() Method
  • Using the spread operator (…) and Math.floor():
  • Using Array.prototype.find Method

Using includes() Method

If the array contains an object/element can be determined by using the includes() method. This method returns true if the array contains the object/element else return false.

Syntax: 

array.includes( element/object, startingPosition )

Example: This example shows the use of the above-explained approach.

Javascript
let obj = { "Beginner1": 10, "Beginner2": 12 }
let gfg = ["Beginner1", "Beginner2", obj];

// Use JavaScript includes() method
let num = gfg.includes(obj, 0);

console.log(num)

Output
true

Using some() Method

The some() method uses the function for its evaluation and it executes the function once for each element present in the array. If it finds the object/element in the array then it returns true and stops the execution for the remaining elements otherwise returns false.

Syntax:  

array.some( function(currValue, arrIndex, arrObj), this )

Example: This example shows the use of the above-explained approach.

Javascript
let arr = ["Beginner1", "Beginner2", "Beginner3",
    { 1: "Beginner4", 2: "Beginner5" }];

let boolVar = arr.some(
    value => { return typeof value == "object" });

console.log(boolVar);

Output
true

Using filter() Method

The filter() method creates the array of all those elements/objects that pass the checking condition.

Syntax: 

array.filter( function(currValue, arrIndex, arrObj), this )

Example: This example shows the use of the above-explained approach.

Javascript
let obj = { "Beginner1": 10, "Beginner2": 12 }
let arr = ["Beginner1", "Beginner2", "Beginner3", obj];

if (arr.filter(value => value == obj).length > 0)
    console.log("true");
else
    console.log("false");

Output
true

Using findIndex() Method

The findIndex() method returns the position of searched object/element if it is present in the array and stops the execution for the rest elements. If the element/object is not found then return -1.

Syntax: 

array.findIndex( function(currValue, arrIndex, arrObj), this )

Example: This example shows the use if the above-explained approach.

Javascript
let arr = ["Beginner1", "Beginner2", "Beginner3",
    { "Beginner1": 10, "Beginner2": 12 }];

let num = arr.findIndex(
    value => { return typeof value == "object" });

console.log("Object found at position: " + num);

Output
Object found at position: 3

Using Lodash _.find() Method

we are using _.find() method which helps us to find out that the given value is present in the array or not. and lodash is third party and javascript library.

Syntax:

_.find(collection, predicate, [fromIndex=0])

Example: This example shows the use if the above-explained approach.

Javascript
// Requiring the lodash library 
const _ = require("lodash");

// Collection of string
let arr = ["Beginner1", "Beginner2", "Beginner3",
    { "Beginner1": 10, "Beginner2": 12 }];
let obj = { "Beginner1": 10, "Beginner2": 12 }

// Check value is found 
// or not by _.includes() method
console.log(_.find(arr, obj));

Output:

{ "Beginner1": 10, "Beginner2": 12 }

Using the spread operator (…) and Math.floor():

In this approach, we utilize the spread operator (…) to create a copy of the original array. Then, we generate a random index within the range of the array length using Math.floor() and Math.random(). Finally, we return the randomly selected element from the copied array.

Example: In this example The function getRandomElement() returns a randomly selected element from an array by creating a copy, generating a random index, and accessing the element at that index.

JavaScript
let arr = ["apple", "banana", "orange", "grape", "kiwi"];

function getRandomElement(arr) {
    // Create a copy of the original array using the spread operator
    let copyArr = [...arr];
    // Generate a random index within the range of the array length
    let randomIndex = Math.floor(Math.random() * copyArr.length);
    // Return the randomly selected element
    return copyArr[randomIndex];
}

console.log(getRandomElement(arr));

Output
grape

Using Array.prototype.find Method

Another approach to check if an array includes an object in JavaScript is by using the find method. This method iterates over the array and returns the first element that satisfies the provided testing function. If no elements satisfy the testing function, it returns undefined.

Example: In this example, we will use the find method to check if the array includes the specified object.

JavaScript
let obj = { "Beginner1": 10, "Beginner2": 12 };
let arr = ["Beginner1", "Beginner2", "Beginner3", obj];

// Use JavaScript find() method
let found = arr.find(element => element === obj);

if (found !== undefined) {
    console.log("Object found in the array");
} else {
    console.log("Object not found in the array");
}

Output
Object found in the array




Contact Us