DEV Community

Barrios Freddy
Barrios Freddy

Posted on

4 1

Array methods: findIndex

The findIndex receives a function (predicate) that accepts three arguments and returns a value that is coercible to the Boolean value true or false.

The predicate returns true. If such an element is found, findIndex immediately returns the index of the first element value. Otherwise, findIndex returns -1, indicating that no element passed the test.

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

const preschoolerIndex = array.findIndex(kid => kid.age < 5);

console.log(preschoolerIndex);
// 1

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.

The findIndex function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

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