Understanding the find() method to search for a specific record in JavaScript?


To fetch a specific record, use find() with some condition.

Example

Following is the code −

var obj=[
   {
      studentId:101,
      studentName:"John"
   },
   {
      studentId:102,
      studentName:"Bob"
   },
   {
      studentId:103,
      studentName:"David"
   }
]
const result = obj.find(
   (o) =>
   {
      return o.studentId === 102
   }
);
console.log(result);

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

node fileName.js.

Here, my file name is demo315.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo315.js
{ studentId: 102, studentName: 'Bob' }

Updated on: 26-Oct-2020

288 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements