ES6 - Array.find



This function returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

Syntax

Given below is the syntax for the array method find(), where, thisArg is an optional object to use as this when executing the callback and callback is the function to execute on each value in the array, taking three arguments as follows −

  • element − The current element being processed in the array.

  • index − This is optional; refers to the index of the current element being processed in the array.

  • array − This is optional; the array on which find was called.

arr.find(callback(element[, index[, array]])[, thisArg])

Example

<script>
   //find
   const products = [{name:'Books',quantity:10},
      {name:'Pen',quantity:20},
      {name:"Books",quantity:30}
   ]
   console.log( products.find(p=>p.name==="Books"))
</script>

The output of the above code will be as mentioned below −

{name: "Books", quantity: 10}
Advertisements