|
| 1 | +# ECMAScript: Array |
| 2 | + |
| 3 | +## Modules |
| 4 | +[`es.array.from`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.from.js), [`es.array.from-async`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.from-async.js), [`es.array.is-array`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.is-array.js), [`es.array.of`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.of.js), [`es.array.copy-within`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.copy-within.js), [`es.array.fill`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.fill.js), [`es.array.find`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.find.js), [`es.array.find-index`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.find-index.js), [`es.array.find-last`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.find-last.js), [`es.array.find-last-index`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.find-last-index.js), [`es.array.iterator`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.iterator.js), [`es.array.includes`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.includes.js), [`es.array.push`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.push.js), [`es.array.slice`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.slice.js), [`es.array.join`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.join.js), [`es.array.unshift`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.unshift.js), [`es.array.index-of`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.index-of.js), [`es.array.last-index-of`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.last-index-of.js), [`es.array.every`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.every.js), [`es.array.some`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.some.js), [`es.array.for-each`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.for-each.js), [`es.array.map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.map.js), [`es.array.filter`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.filter.js), [`es.array.reduce`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.reduce.js), [`es.array.reduce-right`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.reduce-right.js), [`es.array.reverse`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.reverse.js), [`es.array.sort`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.sort.js), [`es.array.flat`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.flat.js), [`es.array.flat-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.flat-map.js), [`es.array.unscopables.flat`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.unscopables.flat.js), [`es.array.unscopables.flat-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.unscopables.flat-map.js), [`es.array.at`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.at.js), [`es.array.to-reversed`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.to-reversed.js), [`es.array.to-sorted`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.to-sorted.js), [`es.array.to-spliced`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.to-spliced.js), [`es.array.with`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.array.with.js). |
| 5 | + |
| 6 | +```ts |
| 7 | +class Array { |
| 8 | + at(index: int): any; |
| 9 | + concat(...args: Array<mixed>): Array<mixed>; // with adding support of @@isConcatSpreadable and @@species |
| 10 | + copyWithin(target: number, start: number, end?: number): this; |
| 11 | + entries(): Iterator<[index, value]>; |
| 12 | + every(callbackfn: (value: any, index: number, target: any) => boolean, thisArg?: any): boolean; |
| 13 | + fill(value: any, start?: number, end?: number): this; |
| 14 | + filter(callbackfn: (value: any, index: number, target: any) => boolean, thisArg?: any): Array<mixed>; // with adding support of @@species |
| 15 | + find(callbackfn: (value: any, index: number, target: any) => boolean), thisArg?: any): any; |
| 16 | + findIndex(callbackfn: (value: any, index: number, target: any) => boolean, thisArg?: any): uint; |
| 17 | + findLast(callbackfn: (value: any, index: number, target: any) => boolean, thisArg?: any): any; |
| 18 | + findLastIndex(callbackfn: (value: any, index: number, target: any) => boolean, thisArg?: any): uint; |
| 19 | + flat(depthArg?: number = 1): Array<mixed>; |
| 20 | + flatMap(mapFn: (value: any, index: number, target: any) => any, thisArg: any): Array<mixed>; |
| 21 | + forEach(callbackfn: (value: any, index: number, target: any) => void, thisArg?: any): void; |
| 22 | + includes(searchElement: any, from?: number): boolean; |
| 23 | + indexOf(searchElement: any, from?: number): number; |
| 24 | + join(separator: string = ','): string; |
| 25 | + keys(): Iterator<index>; |
| 26 | + lastIndexOf(searchElement: any, from?: number): number; |
| 27 | + map(mapFn: (value: any, index: number, target: any) => any, thisArg?: any): Array<mixed>; // with adding support of @@species |
| 28 | + push(...args: Array<mixed>): uint; |
| 29 | + reduce(callbackfn: (memo: any, value: any, index: number, target: any) => any, initialValue?: any): any; |
| 30 | + reduceRight(callbackfn: (memo: any, value: any, index: number, target: any) => any, initialValue?: any): any; |
| 31 | + reverse(): this; // Safari 12.0 bug fix |
| 32 | + slice(start?: number, end?: number): Array<mixed>; // with adding support of @@species |
| 33 | + splice(start?: number, deleteCount?: number, ...items: Array<mixed>): Array<mixed>; // with adding support of @@species |
| 34 | + some(callbackfn: (value: any, index: number, target: any) => boolean, thisArg?: any): boolean; |
| 35 | + sort(comparefn?: (a: any, b: any) => number): this; // with modern behavior like stable sort |
| 36 | + toReversed(): Array<mixed>; |
| 37 | + toSpliced(start?: number, deleteCount?: number, ...items: Array<mixed>): Array<mixed>; |
| 38 | + toSorted(comparefn?: (a: any, b: any) => number): Array<mixed>; |
| 39 | + unshift(...args: Array<mixed>): uint; |
| 40 | + values(): Iterator<value>; |
| 41 | + with(index: includes, value: any): Array<mixed>; |
| 42 | + @@iterator(): Iterator<value>; |
| 43 | + @@unscopables: { [newMethodNames: string]: true }; |
| 44 | + static from(items: Iterable | ArrayLike, mapFn?: (value: any, index: number) => any, thisArg?: any): Array<mixed>; |
| 45 | + static fromAsync(asyncItems: AsyncIterable | Iterable | ArrayLike, mapfn?: (value: any, index: number) => any, thisArg?: any): Array; |
| 46 | + static isArray(value: any): boolean; |
| 47 | + static of(...args: Array<mixed>): Array<mixed>; |
| 48 | +} |
| 49 | + |
| 50 | +class Arguments { |
| 51 | + @@iterator(): Iterator<value>; // available only in core-js methods |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## [CommonJS entry points]({docs-version}/docs/usage#commonjs-api) |
| 56 | +``` |
| 57 | +core-js(-pure)/es|stable|actual|full/array |
| 58 | +core-js(-pure)/es|stable|actual|full/array/from |
| 59 | +core-js(-pure)/es|stable|actual|full/array/from-async |
| 60 | +core-js(-pure)/es|stable|actual|full/array/of |
| 61 | +core-js(-pure)/es|stable|actual|full/array/is-array |
| 62 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/at |
| 63 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/concat |
| 64 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/copy-within |
| 65 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/entries |
| 66 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/every |
| 67 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/fill |
| 68 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/filter |
| 69 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/find |
| 70 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/find-index |
| 71 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/find-last |
| 72 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/find-last-index |
| 73 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/flat |
| 74 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/flat-map |
| 75 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/for-each |
| 76 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/includes |
| 77 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/index-of |
| 78 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/iterator |
| 79 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/join |
| 80 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/keys |
| 81 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/last-index-of |
| 82 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/map |
| 83 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/push |
| 84 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/reduce |
| 85 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/reduce-right |
| 86 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/reverse |
| 87 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/slice |
| 88 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/some |
| 89 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/sort |
| 90 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/splice |
| 91 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/to-reversed |
| 92 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/to-sorted |
| 93 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/to-spliced |
| 94 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/unshift |
| 95 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/values |
| 96 | +core-js(-pure)/es|stable|actual|full/array(/virtual)/with |
| 97 | +``` |
| 98 | + |
| 99 | +## Examples |
| 100 | +```js |
| 101 | +Array.from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3] |
| 102 | +Array.from({ 0: 1, 1: 2, 2: 3, length: 3 }); // => [1, 2, 3] |
| 103 | +Array.from('123', Number); // => [1, 2, 3] |
| 104 | +Array.from('123', it => it ** 2); // => [1, 4, 9] |
| 105 | + |
| 106 | +Array.of(1); // => [1] |
| 107 | +Array.of(1, 2, 3); // => [1, 2, 3] |
| 108 | + |
| 109 | +let array = ['a', 'b', 'c']; |
| 110 | + |
| 111 | +for (let value of array) console.log(value); // => 'a', 'b', 'c' |
| 112 | +for (let value of array.values()) console.log(value); // => 'a', 'b', 'c' |
| 113 | +for (let key of array.keys()) console.log(key); // => 0, 1, 2 |
| 114 | +for (let [key, value] of array.entries()) { |
| 115 | + console.log(key); // => 0, 1, 2 |
| 116 | + console.log(value); // => 'a', 'b', 'c' |
| 117 | +} |
| 118 | + |
| 119 | +function isOdd(value) { |
| 120 | + return value % 2; |
| 121 | +} |
| 122 | +[4, 8, 15, 16, 23, 42].find(isOdd); // => 15 |
| 123 | +[4, 8, 15, 16, 23, 42].findIndex(isOdd); // => 2 |
| 124 | +[1, 2, 3, 4].findLast(isOdd); // => 3 |
| 125 | +[1, 2, 3, 4].findLastIndex(isOdd); // => 2 |
| 126 | + |
| 127 | +Array(5).fill(42); // => [42, 42, 42, 42, 42] |
| 128 | + |
| 129 | +[1, 2, 3, 4, 5].copyWithin(0, 3); // => [4, 5, 3, 4, 5] |
| 130 | + |
| 131 | +[1, 2, 3].includes(2); // => true |
| 132 | +[1, 2, 3].includes(4); // => false |
| 133 | +[1, 2, 3].includes(2, 2); // => false |
| 134 | + |
| 135 | +[NaN].indexOf(NaN); // => -1 |
| 136 | +[NaN].includes(NaN); // => true |
| 137 | +Array(1).indexOf(undefined); // => -1 |
| 138 | +Array(1).includes(undefined); // => true |
| 139 | + |
| 140 | +[1, [2, 3], [4, 5]].flat(); // => [1, 2, 3, 4, 5] |
| 141 | +[1, [2, [3, [4]]], 5].flat(); // => [1, 2, [3, [4]], 5] |
| 142 | +[1, [2, [3, [4]]], 5].flat(3); // => [1, 2, 3, 4, 5] |
| 143 | + |
| 144 | +[{ a: 1, b: 2 }, { a: 3, b: 4 }, { a: 5, b: 6 }].flatMap(it => [it.a, it.b]); // => [1, 2, 3, 4, 5, 6] |
| 145 | + |
| 146 | +[1, 2, 3].at(1); // => 2 |
| 147 | +[1, 2, 3].at(-1); // => 3 |
| 148 | + |
| 149 | +const sequence = [1, 2, 3]; |
| 150 | +sequence.toReversed(); // => [3, 2, 1] |
| 151 | +sequence; // => [1, 2, 3] |
| 152 | + |
| 153 | +const initialArray = [1, 2, 3, 4]; |
| 154 | +initialArray.toSpliced(1, 2, 5, 6, 7); // => [1, 5, 6, 7, 4] |
| 155 | +initialArray; // => [1, 2, 3, 4] |
| 156 | + |
| 157 | +const outOfOrder = [3, 1, 2]; |
| 158 | +outOfOrder.toSorted(); // => [1, 2, 3] |
| 159 | +outOfOrder; // => [3, 1, 2] |
| 160 | + |
| 161 | +const correctionNeeded = [1, 1, 3]; |
| 162 | +correctionNeeded.with(1, 2); // => [1, 2, 3] |
| 163 | +correctionNeeded; // => [1, 1, 3] |
| 164 | +``` |
| 165 | + |
| 166 | +## `Array.fromAsync` example |
| 167 | +```ts |
| 168 | +await Array.fromAsync((async function * () { yield * [1, 2, 3]; })(), i => i ** 2); // => [1, 4, 9] |
| 169 | +``` |
0 commit comments