DEV Community

Barrios Freddy
Barrios Freddy

Posted on • Updated on

Array methods: find

The find method receives a callback function that is used as a testing function and, optionally, an object to use as this inside. This returns the value of the first element in the provided array that satisfies the testing. Otherwise, the find method returns undefined.

const array = [
 {name: 'Emilia', age: 4},
 {name: 'Freddy', age: 10}
];

const preschooler = array.find(kid => kid.age < 5);

console.log(preschooler);
// { name: 'Emilia', age: 4 }
Enter fullscreen mode Exit fullscreen mode

Parameters

  1. callback
    Testing function to execute on each value in the array, taking 3 arguments:

    element
    The current element in the array.
    index (Optional)
    The index (position) of the current element in the array.
    array (Optional)
    The array that find was called on.

  2. thisArg (Optional)
    Object to use as this inside callback.

find is a method very useful in order to encounter a specific element into an array, providing a function that will be invoked on each element until finding the value that satisfies the testing. Therefore, be careful, the usage of this method in some cases can be inefficient.

Top comments (0)