Function polyArrFindLastIndex

  • The polyArrFindLastIndex() method iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.

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

    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.

    polyArrFindLastIndex() 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 polyArrFindLastIndex() 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.

    • OptionalthisArg: any

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

    Returns number

    The index of the last (highest-index) element in the array that passes the test. Otherwise -1 if no matching element is found.

    0.8.0