• The arrFind() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

    • If you need the index of the found element in the array, use arrFindIndex.
    • If you need to find the index of a value, use arrIndexOf(). (It's similar to arrFindIndex, but checks each element for equality with the value instead of using a testing function.)
    • If you need to find if a value exists in an array, use arrIncludes. Again, it checks each element for equality with the value instead of using a testing function.
    • If you need to find if any element satisfies the provided testing function, use arrSome.

    The arrFind() method is an iterative method. It calls a provided callbackFn function once for each element in an array in ascending-index order, until callbackFn returns a truthy value. arrFind() then returns that element and stops iterating through the array. If callbackFn never returns a truthy value, arrFind() returns undefined.

    callbackFn is invoked for every index of the array, not just those with assigned values. Empty slots in sparse arrays behave the same as undefined.

    arrFind() does not mutate the array on which it is called, but the function provided as callbackFn can. Note, however, that the length of the array is saved before the first invocation of callbackFn. Therefore:

    • callbackFn will not visit any elements added beyond the array's initial length when the call to arrFind() began.
    • Changes to already-visited indexes do not cause callbackFn to be invoked on them again.
    • If an existing, yet-unvisited element of the array is changed by callbackFn, its value passed to the callbackFn will be the value at the time that element gets visited. Deleted elements are visited as if they were undefined.

    Type Parameters

    • T

      Identifies the base type of array elements

    • E

      Identifies a more specific instance of the base array type

    Parameters

    • theArray: ArrayLike<T>

      The array or array like object of elements to be searched.

    • callbackFn: ArrPredicateCallbackFn<T, E> | ArrPredicateCallbackFn2<T>

      A function that accepts up to three arguments of type ArrPredicateCallbackFn or ArrPredicateCallbackFn2. The predicate function is called for each element in the thArray until the predicate returns a value which is coercible to the Boolean value false, or until the end of the array.

    • Optional thisArg: any

      A value to use as this when executing callbackFn. Defaults to the array if not provided.

    Returns T | E

    The first element in the array that satisfies the provided testing function. Otherwise, undefined is returned.

    Since

    0.8.0

    Example

    const inventory = [
    { name: "apples", quantity: 2 },
    { name: "bananas", quantity: 0 },
    { name: "cherries", quantity: 5 },
    ];

    function isCherries(fruit) {
    return fruit.name === "cherries";
    }

    console.log(arrFind(inventory, isCherries));
    // { name: 'cherries', quantity: 5 }

    function isPrime(element, index, array) {
    let start = 2;
    while (start <= Math.sqrt(element)) {
    if (element % start++ < 1) {
    return false;
    }
    }
    return element > 1;
    }

    console.log(arrFind([4, 6, 8, 12], isPrime)); // undefined, not found
    console.log(arrFind([4, 5, 8, 12], isPrime)); // 5

    // Array Like
    console.log(arrFind({ length: 4, 0: 4, 1: 6, 2: 8, 3: 12 }, isPrime)); // undefined, not found
    console.log(arrFind({ length: 4:, 0: 4, 1: 5, 2: 8, 3: 12 }, isPrime)); // 5