Saturday, September 21, 2024
Google search engine
HomeLanguagesHow to check if an array includes an object in JavaScript ?

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

In this article, 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.

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 = {"neveropen1": 10, "neveropen2": 12}
let gfg = ["neveropen1", "neveropen2", 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 = ["neveropen1", "neveropen2", "neveropen3",
            {1: "neveropen4", 2: "neveropen5"}];
             
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 = { "neveropen1": 10, "neveropen2": 12 }
let arr = ["neveropen1", "neveropen2", "neveropen3", 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 = ["neveropen1", "neveropen2", "neveropen3",
    { "neveropen1": 10, "neveropen2": 12 }];
 
let num = arr.findIndex(
    value => { return typeof value == "object" });
 
console.log("Object found at position: " + num);


Output

Object found at position: 3

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments