DEV Community

Barrios Freddy
Barrios Freddy

Posted on • Edited on

3 2

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.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more