跳至主要內容

通过复制来改变 Array

小于 1 分钟featurees-proposal

通过复制来改变 Arrayopen in new window

模块

类型

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;
}

入口点

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

示例

示例open 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]