Hello Friends đź‘‹,

Welcome To Infinitbility! ❤️

This tutorial will help you to check value exist in array of objects in javascript, for check value exist or not we will use below methods.

  1. Javascript findIndex() method
  2. Javascript find() method

Let’s start today’s tutorial How to check value exist in array of object in javascript?

Javascript findIndex() method

Javascript provide findIndex() method to find value in array, and we can also use this method to check value exist or not in array of objects.

Let take below users array of objects for example.

const users = [
  {
    id: 1,
    name: "Infinit"
  },{
    id: 2,
    name: "Bility"
  },{
    id: 3,
    name: "Infinitbility"
  }
];

let check users exist or not using user id value.

const users = [
  {
    id: 1,
    name: "Infinit"
  },{
    id: 2,
    name: "Bility"
  },{
    id: 3,
    name: "Infinitbility"
  }
];

let userIndex = users.findIndex(e => e.id == 2);
console.log(userIndex);  // 1
if(userIndex >= 0){
  console.log("Exist");
} else {
  console.log("Not exist");
}

javascript findIndex() method return index of object if value match else it will return -1.

output

findIndex, Javascript, Example

Javascript find() method

Javascript find() method same work like findIndex but it will return whole object or array value if match else it will return undefined.

const users = [
  {
    id: 1,
    name: "Infinit"
  },{
    id: 2,
    name: "Bility"
  },{
    id: 3,
    name: "Infinitbility"
  }
];

let user = users.find(e => e.id == 2);
console.log(user);  // { id: 2, name: 'Bility' }
if(user){
  console.log("Exist");
} else {
  console.log("Not exist");
}

output

find, Javascript, Example