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.

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

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay