Fetch specific values from array of objects in JavaScript?


Let’s say the following are our array of objects:

const details =
   [
      {
         employeeFirstName: "John",
         employeeLastName: "Doe"
      },
      {
         employeeFirstName: "David",
         employeeLastName: "Miller"
      },
      {
         employeeFirstName: "John",
         employeeLastName: "Smith"
      }
   ]

Example

Following is the code to fetch specific values, in this case with first name “John” −

const details =
   [
      {
            employeeFirstName: "John",
            employeeLastName: "Doe"
      },
      {
         employeeFirstName: "David",
         employeeLastName: "Miller"
      },
      {
         employeeFirstName: "John",
         employeeLastName: "Smith"
      }
   ]
for (var index = 0; index < details.length; index++) {
   if (details[index].employeeFirstName === "John") {
      console.log("FirstName=" + details[index].employeeFirstName + " LastName= " + details[index].employeeLastName);
   }
}

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo223.js.

Output

The output is as follows −

PS C:\Users\Amit\JavaScript-code> node demo223.js
FirstName=John LastName= Doe
FirstName=John LastName= Smith

Updated on: 03-Oct-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements