uft
    Preparing search index...

    Function removeFirst

    • Finds the first element that matches a predicate and removes it by mutating the array in-place.

      Type Parameters

      • T extends unknown[]
      • R extends boolean | undefined = undefined

      Parameters

      • array: T

        The array to remove the element from.

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

        The predicate to match the element against.

      • OptionalreturnIndex: R

        Whether to return the index the removed element was found at.

      Returns Removed<T, R>

      undefined if no element was removed. Otherwise, returns the removed element or an object with the removed element and index if returnIndex is true.

      const array = [1, 2, 3, 4, 5]

      const r1 = removeFirst(array, (v) => v === 3)
      console.log(array, r1) // [1, 2, 4, 5] , 3

      const r2 = removeFirst(array, (v) => v === 6)
      console.log(array, r2) // [1, 2, 4, 5] , undefined

      const r3 = removeFirst(array, (v) => v > 1)
      console.log(array, r3) // [1, 4, 5] , 2
      const array = [1, 2, 3, 4, 5]

      // Pass `true` as the third argument.
      const r1 = removeFirst(array, (v) => v === 3, true)
      console.log(array, r1) // [1, 2, 4, 5] , { item: 3, index: 2 }

      const r2 = removeFirst(array, (v) => v === 6, true)
      console.log(array, r2) // [1, 2, 4, 5] , undefined