Tuesday, 12 June 2018

ES6 - Arrow function with Array prototype find()

Hi Folks,

Today we are going see Arrow function using find() in Javascript.

find() method search the array of objects  and returns the value of the first element in the array that satisfied condition otherwise it will return undefined

Using this find() we should not to loop the entire object and it will give the result very fast compare to traditional looping.

Example:

var empData = [{"name":"Nookala",  "age": 30},
              {"name":"Gunti",  "age": 27},
              {"name":"Teki", "age": 25 },
              {"name":"Yennam", "age": 42 }];


Now are going to search positive scenario

var result = empData.find(o => o.name === "Nookala");

Result will be


If There is no data in object.





Source: ECMA






No comments:

Post a Comment