JavaScript find if an object is in an array of object

Published by admin on

JavaScript find if an object is in an array

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 little bit tricky. 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. JavaScript provides a couple of different methods that makes it more easier.

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 :

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

Our idea is to filter out all elements those 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 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 atleast one element passes the function. 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 is testing if any object in the array is available with id and name equal to the given object. If found, it returns true and moves inside the block.

 

Using findIndex() :

findIndex() method is used to find the index of 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 way 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 new value for each element of the array using this function. We can create one boolean array by checking every element if it is equal to the provided object or not. Next, use includes to check if that array include 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 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..");
}
Categories: javascript

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *