uft
    Preparing search index...

    Function replaceArray

    • Removes all elements from the target array and inserts new elements in their place. The target array will be mutated in-place.

      Type Parameters

      • T extends unknown[]

      Parameters

      • target: T

        The array to replace.

      • replaceWith: T

        The elements to insert.

      Returns T

      An array containing the elements that were removed.

      const array = [1, 2]
      const replaceWith = [3, 4]
      const removed = replaceArray(array, replaceWith)
      console.log(array, removed) // [3, 4] , [1, 2]
      // replaceWith is not mutated
      console.log(replaceWith) // [3, 4]

      The replaceWith array can be a different size from the target array.

      // replaceWith is smaller than target
      const array = [1, 2]
      const removed = replaceArray(array, [3])
      console.log(array, removed) // [3] , [1, 2]
      // replaceWith is larger than target
      const array = [1]
      const removed = replaceArray(array, [2, 3])
      console.log(array, removed) // [2, 3] , [1]