How to find an object in an Array of objects in JavaScript

collegewap

collegewap

Posted on April 6, 2023

How to find an object in an Array of objects in JavaScript

Do you want to find a record from an array of records using an identifier or a unique field?
Do you want to filter out records matching a certain condition? We will explore all of them in this tutorial.

Consider the following array of employees:

const employees = [
  {
    id: 1,
    first_name: "Jonas",
    last_name: "Baudasso",
    email: "jbaudasso0@paypal.com",
    gender: "Male",
    department: "Support",
  },
  {
    id: 2,
    first_name: "Delcina",
    last_name: "Baldelli",
    email: "dbaldelli1@istockphoto.com",
    gender: "Female",
    department: "Engineering",
  },
  {
    id: 3,
    first_name: "Charlotta",
    last_name: "Sodor",
    email: "csodor2@businessinsider.com",
    gender: "Female",
    department: "Human Resources",
  },
  {
    id: 4,
    first_name: "Scarface",
    last_name: "Sandercock",
    email: "ssandercock3@ovh.net",
    gender: "Male",
    department: "Support",
  },
  {
    id: 5,
    first_name: "Rob",
    last_name: "Beurich",
    email: "rbeurich4@comsenz.com",
    gender: "Male",
    department: "Engineering",
  },
]
Enter fullscreen mode Exit fullscreen mode

We will use the above list in our examples.

Finding the index of the object

We can use the findIndex
method to get the index of the first matching record.

const index = employees.findIndex(employee => {
  // Function gets called for each employee until the function returns true
  if (employee.id === 3) {
    return true
  }
  return false
})

console.log(index) // ๐Ÿ‘‰ 2

const employee = employees[index]

console.log(employee?.first_name) //๐Ÿ‘‰ Charlotta
Enter fullscreen mode Exit fullscreen mode

We can simplify the findIndex function callback to a single line:

const index = employees.findIndex(employee => employee.id === 3)
Enter fullscreen mode Exit fullscreen mode

If we pass an id that does not exist, the findIndex function will return -1.

Finding the object directly

We can use the find
method to get the object directly without finding the index explicitly.

const employee = employees.find(employee => employee.id === 3)

console.log(employee?.first_name) //๐Ÿ‘‰ Charlotta
Enter fullscreen mode Exit fullscreen mode

If the employee is not found, the find method will return undefined.

Finding using a unique key

It is not necessary that we always need to use an id to find the object. We can use other unique fields like email as well:

const employee = employees.find(
  employee => employee.email === "rbeurich4@comsenz.com"
)
console.log(employee?.first_name) // ๐Ÿ‘‰ Rob
Enter fullscreen mode Exit fullscreen mode

If you check for a non-unique in the condition, then it will return the first matching record.

Finding all the records which match the condition

If you want to filter all the records matching a certain condition, then you can use the filter method as shown below:

const engineers = employees.filter(
  employee => employee.department === "Engineering"
)
console.log(engineers.length) // ๐Ÿ‘‰ 2
Enter fullscreen mode Exit fullscreen mode

The filter function returns an array of objects with matching records.
If there are no matching records, then it returns an empty array.

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
collegewap
collegewap

Posted on April 6, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About