Skip to main content

Change Array by copy

Less than 1 minutefeaturees-proposal

Change Array by copyopen in new window

Modules

Types

interface Array<T> {
  toReversed(): Array<T>;
  toSpliced(start?: number, deleteCount?: number, ...items: Array<T>): Array<T>;
  toSorted(comparefn?: (a: T, b: T) => number): Array<T>;
  with(index: number, value: T): Array<T>;
}

interface TypedArray {
  toReversed(): TypedArray;
  toSorted(comparefn?: (a: number, b: number) => number): TypedArray;
  with(index: number, value: number): TypedArray;
}

Entry points

core-js/proposals/change-array-by-copy
core-js(-pure)/actual|full/array(/virtual)/to-reversed
core-js(-pure)/actual|full/array(/virtual)/to-sorted
core-js(-pure)/actual|full/array(/virtual)/to-spliced
core-js(-pure)/actual|full/array(/virtual)/with
core-js/actual|full/typed-array/to-reversed
core-js/actual|full/typed-array/to-sorted
core-js/actual|full/typed-array/with

Example

Exampleopen in new window:

const sequence = [1, 2, 3];
sequence.toReversed(); // => [3, 2, 1]
sequence; // => [1, 2, 3]

const array = [1, 2, 3, 4];
array.toSpliced(1, 2, 5, 6, 7); // => [1, 5, 6, 7, 4]
array; // => [1, 2, 3, 4]

const outOfOrder = [3, 1, 2];
outOfOrder.toSorted(); // => [1, 2, 3]
outOfOrder; // => [3, 1, 2]

const correctionNeeded = [1, 1, 3];
correctionNeeded.with(1, 2); // => [1, 2, 3]
correctionNeeded; // => [1, 1, 3]