swap(start, length, values?)

Replaces a length of values from a starting position with the given values.

Unlike splice, if the start index is beyond the length of an array, swap will extend the array to the given start index, leaving holes between the old length and the start index.

In version 2, swap no longer returns an array of the removed length of values, which further distinguishes it from splice, making it less wasteful in some cases.

On collections

Usage

  • swap(start, length)
  • swap(start, length, values)

Related

  • splice(start, length, ...values)

    Replaces a length of values from a starting position with the given variadic values, and returns the values that were replaced as an array.

  • slice(start?, end?)

    Returns an array of the values contained in the half-open interval [start, end), that is, including the start and excluding the end.