How to Check If an Array Includes an Object in JavaScript

Here are the ways to check if an array includes an object in JavaScript:

  1. Using the “array.includes()” function
  2. Using the “array.some()” function
  3. Using the “array.filter()” function
  4. Using the “array.findIndex()” function
  5. Using the “array.find()” function

Method 1: Using the array.includes() function

You can use the “array.includes()” method to check if an array contains an object. If the array contains an object, it returns true otherwise false.

Syntax

array.includes( element/object, starting_position)

Example

let obj_one = { "name": "krunal", "rollno": 21 }
let arr = ["obj_two", "obj_three", obj_one];

let contains = arr.includes(obj_one);

console.log(contains)

Output

true

You can see that an array arr contains an obj_one object. Let’s see an example where the array does not contain an object.

let obj_one = { "name": "Krunal", "rollno": 21 }
let obj_five = { "name": "Khushi", "rollno": 19 }

let arr = ["obj_two", "obj_three", obj_one];

let contains = arr.includes(obj_five);
console.log(contains)

Output

false

Method 2: Using the array.some() function

You can use the “array.some()” method to check if an array contains an object that passes a test function. The method returns true if any element in the array satisfies the test function and false otherwise.

Syntax

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

Example

let people = [
  { name: "Krunal", age: 30 },
  { name: "Ankit", age: 28 },
  { name: "Rushabh", age: 32 }
];

let result = people.some(person => person.name === "Ankit");
console.log(result);

Output

true

It returns true because the “Ankit” property object exists in the array. If it did not, then it would have returned false.

Method 3: Using the array.filter() function

You can use the “array.filter()” method to check if an array contains an object that matches a condition. The method returns a new array with only the elements that pass the condition. If it returns an empty array, it does not contain an object.

Syntax

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

Example

let list = [{name: 'Krunal'}, {name: 'Ankit'}];

let outpt = list.filter(data => (data.name == 'Krunal'));

console.log(outpt)

Output

[ { name: 'Krunal' } ]

That means an array contains an object because it returns a property of the object that matches the criteria.

Method 4: Using the array.findIndex() function

You can use the “array.findIndex()” method to check if an array contains an object that matches a condition. The method returns the index of the first element that passes the condition, or -1 if none.

Syntax

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

Example

let people = [
  { name: "Krunal", age: 30 },
  { name: "Ankit", age: 28 },
  { name: "Rushabh", age: 32 }
];

let result = people.findIndex(person => person.name === "Rushabh");
console.log(result);

Output

2

It returns index 2, which means an array contains an object.

An important thing to note is that Array.findIndex() method does not return a boolean value like Array.some() method. So, if you only want to know whether an object exists, you can use Array.some() method instead.

Method 5: Using the array.find() function

You can use the array.find() method to check if an array contains an object that matches a condition. The method returns the first element that passes the condition or is “undefined” if none.

Syntax

array.find(function(currentValue, index, arr),thisValue)

Example

let people = [
  { name: "Krunal", age: 30 },
  { name: "Ankit", age: 28 },
  { name: "Rushabh", age: 32 }
];

let result = people.find(person => person.name === "Krunal");
console.log(result);

Output

{ name: 'Krunal', age: 30 }

You can see that an array contains an object with a property “name” whose value is “Krunal”. That’s why it returns the object; if it does not have it, it returns “undefined”.

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.