How to Check If an Array Includes an Object in JavaScript

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

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

Method 1: Using the includes() function

The array.includes() method returns true  if an array contains an object, otherwise false.

Syntax

array.includes( element/object, starting_position)

Visual Representation

Using the includes() function

Example

let obj = { "name": "david", "age": 30 }
let arr = ["obj_two", "obj_three", obj];

let contains = arr.includes(obj);

console.log(contains)

Output

true

Method 2: Using the some() function

The array.some() method returns true if any element in the array satisfies the test function and false otherwise.

Syntax

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

Visual Representation

Visual Representation of Using the some() function

Example

let people = [
  { name: "David", age: 30 },
  { name: "Ron", age: 28 },
  { name: "Jon`", age: 32 }
];

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

Output

true

Method 3: Using the filter() function

The array.filter() method returns a new array with only the elements that pass the condition. 

Syntax

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

Visual Representation

Visual Representation of Using the filter() function

Example

let people = [{name: 'David'}, {name: 'Ron'}];

let output = people.filter(data => (data.name == 'David'));

console.log(output)

Output

[ { name: 'David' } ]

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

Method 4: Using the findIndex() function

The array.findIndex() method returns the index of the first element that passes the condition, or -1 if none.

Syntax

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

Visual Representation

Visual Representation of Using the findIndex() function

Example

let people = [
  { name: "David", age: 30 },
  { name: "Ron", age: 28 },
  { name: "Jon", age: 32 }
];

let result = people.findIndex(person => person.name === "Jon");
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 find() function

The array.find() method returns the first element that passes the condition or is “undefined” if none.

Syntax

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

Visual Representation

Visual Representation of Using the find() function

Example

let people = [
  { name: "David", age: 30 },
  { name: "Ron", age: 28 },
  { name: "Jon", age: 32 }
];

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

Output

{ name: 'Ron', age: 28 }

Leave a Comment

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