Function takeWhile

  • Returns a new array containing the leading elements of the provided array that satisfy the provided predicate function. It stops taking elements as soon as an element does not satisfy the predicate.

    Type Parameters

    • T

      The type of elements in the array.

    Parameters

    • arr: readonly T[]

      The array to process.

    • shouldContinueTaking: ((element: T) => boolean)

      The predicate function that is called with each element. Elements are included in the result as long as this function returns true.

        • (element): boolean
        • Parameters

          • element: T

          Returns boolean

    Returns T[]

    A new array containing the leading elements that satisfy the predicate.

    // Returns [1, 2]
    takeWhile([1, 2, 3, 4], x => x < 3);
    // Returns []
    takeWhile([1, 2, 3, 4], x => x > 3);