JavaScript find if an object is in an array of object

JavaScript program to find if an object is in an array or not :

Finding out if an object is in an array or not is a little bit tricky to solve in JavaScript. indexOf doesn’t work for objects. Either you need to use one loop or you can use any other methods provided in ES6. Loop is not a good option. We have other JavaScript methods to solve it easily.

In this post, I will show you different ways to solve this problem. If you know any other way to solve it, please drop one comment below.

Using filter :

The array filter method is used to filter out elements from an array. It takes one function as the parameter and returns the elements for those the function returns true.

Our idea is to filter out all elements that are equal to the given object. We will check the length of the final array. If it is 0, it means the object is in that array.

const givenArr = [
  { id: 0, name: "Alex" },
  { id: 2, name: "Bob" },
  { id: 2, name: "Charlie" },
];
const obj = { id: 2, name: "Charlie" };

if (
  givenArr.filter((e) => e.id === obj.id && e.name === obj.name).length === 0
) {
  console.log("Not Found..");
} else {
  console.log("Found..");
}

Here, the parameter, i.e. the arrow function checks if the id and name of the object obj is equal to the current object or not. If yes, it adds it to the final array. If no item is found, the result array will hold nothing.

Using some() :

some() method takes one function as the parameter and returns one boolean. true if the function returns true at least for one element, and false otherwise.

const givenArr = [
  { id: 0, name: "Alex" },
  { id: 2, name: "Bob" },
  { id: 2, name: "Charlie" },
];
const obj = { id: 2, name: "Charlie" };

if (givenArr.some((e) => e.id === obj.id && e.name === obj.name)) {
  console.log("Found..");
} else {
  console.log("Not found..");
}

Here, the if condition checks if any object in the array is available with id and name equal to the given object. If it finds any, it returns true and moves inside the block.

Using findIndex() :

findIndex() method is used to find the index of the first element in the array based on a function parameter. If the element is not found, it returns -1.

const givenArr = [
  { id: 0, name: "Alex" },
  { id: 2, name: "Bob" },
  { id: 2, name: "Charlie" },
];
const obj = { id: 2, name: "Charlie" };

if (givenArr.findIndex((e) => e.id === obj.id && e.name === obj.name) === -1) {
  console.log("Not found..");
} else {
  console.log("Found...");
}

We are checking if the return value is -1 or not using this approach.

Using find() :

find() is another method to find elements in an array. It returns the first element in an array if it is found. It returns undefined if it is not found.

const givenArr = [
  { id: 0, name: "Alex" },
  { id: 2, name: "Bob" },
  { id: 2, name: "Charlie" },
];
const obj = { id: 2, name: "Charlie" };

if (givenArr.find((e) => e.id === obj.id && e.name === obj.name)) {
  console.log("Found...");
} else {
  console.log("Not found...");
}

Using an if condition, we can check if the element is available or not in the array.

Using map :

map() takes one function and creates one different array by generating a new value for each element of the array using that function. We can create one boolean array by checking every element if it is equal to the provided object or not. Then, we can use includes to check if that array includes true.

const givenArr = [
  { id: 0, name: "Alex" },
  { id: 2, name: "Bob" },
  { id: 2, name: "Charlie" },
];
const obj = { id: 2, name: "Charlie" };

if (
  givenArr.map((e) => e.id === obj.id && e.name === obj.name).includes(true)
) {
  console.log("Found..");
} else {
  console.log("Not found...");
}

You can try to print out the result array to get a better understanding.

Using forEach :

forEach is used to iterate through the array elements one by one. We can keep one flag and based on it, we can print if the object is found or not.

const givenArr = [
  { id: 0, name: "Alex" },
  { id: 2, name: "Bob" },
  { id: 2, name: "Charlie" },
];
const obj = { id: 2, name: "Charlie" };

let isExist = false;
let result = givenArr.forEach((e) => {
  if (e.id === obj.id && e.name === obj.name) {
    isExist = true;
    return false;
  }
});

if (isExist) {
  console.log("Found..");
} else {
  console.log("Not found..");
}

Using for,for…in and for…of :

Similar to forEach, we can use any loop to find out if an object is in an array or not.

Using for loop :

const givenArr = [
  { id: 0, name: "Alex" },
  { id: 2, name: "Bob" },
  { id: 2, name: "Charlie" },
];
const obj = { id: 2, name: "Charlie" };

let isExist = false;
for (var i = 0; i < givenArr.length; i++) {
  if (givenArr[i].id === obj.id && givenArr[i].name === obj.name) {
    isExist = true;
    break;
  }
}

if (isExist) {
  console.log("Found..");
} else {
  console.log("Not found..");
}

Using for…in loop :

const givenArr = [
  { id: 0, name: "Alex" },
  { id: 2, name: "Bob" },
  { id: 2, name: "Charlie" },
];
const obj = { id: 2, name: "Charlie" };

let isExist = false;

for (item in givenArr) {
  if (givenArr[item].id === obj.id && givenArr[item].name === obj.name) {
    isExist = true;
    break;
  }
}

if (isExist) {
  console.log("Found..");
} else {
  console.log("Not found..");
}

Using for…of :

const givenArr = [
  { id: 0, name: "Alex" },
  { id: 2, name: "Bob" },
  { id: 2, name: "Charlie" },
];
const obj = { id: 2, name: "Charlie" };

let isExist = false;

for (item of givenArr) {
  if (item.id === obj.id && item.name === obj.name) {
    isExist = true;
    break;
  }
}

if (isExist) {
  console.log("Found..");
} else {
  console.log("Not found..");
}

You might also like: