uft
    Preparing search index...

    Function removeLast

    • Finds the last 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 = removeLast(array, (v) => v === 3)
      console.log(array, r1) // [1, 2, 4, 5] , 3

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

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

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

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