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

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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' }
raja
Published on 26-Oct-2020 15:44:49
Advertisements