Prelude API
    Preparing search index...

    Function findIndex

    • Creates a function that returns the index of the first element in an iterable that satisfies the predicate. Similar to Array.prototype.findIndex(), but works with any iterable.

      Type Parameters

      • T

        Type of elements in the iterable

      Parameters

      • predicate: (value: T, index: number) => boolean

        Function to test each element and its index

      Returns (values: Iterable<T>) => number

      A function that takes an iterable and returns the index of the first matching element, or -1 if no match is found

      // Find index of first even number
      const firstEvenIndex = findIndex((n: number) => n % 2 === 0)([1, 3, 5, 6, 7, 8])
      // Result: 3
      // Find index of first element greater than 10
      const greaterThan10Index = findIndex((n: number) => n > 10)([1, 5, 8, 12, 15])
      // Result: 3
      // No match returns -1
      const negativeIndex = findIndex((n: number) => n < 0)([1, 2, 3, 4])
      // Result: -1