Array
About 4 min
Array
Modules
es.array.from
es.array.is-array
es.array.of
es.array.copy-within
es.array.fill
es.array.find
es.array.find-index
es.array.find-last
es.array.find-last-index
es.array.iterator
es.array.includes
es.array.push
es.array.slice
es.array.join
es.array.unshift
es.array.index-of
es.array.last-index-of
es.array.every
es.array.some
es.array.for-each
es.array.map
es.array.filter
es.array.reduce
es.array.reduce-right
es.array.reverse
es.array.sort
es.array.flat
es.array.flat-map
es.array.unscopables.flat
es.array.unscopables.flat-map
es.array.at
esnext.array.to-reversed
esnext.array.to-sorted
esnext.array.to-spliced
esnext.array.with
Types
interface Array<T> {
at(index: number): T;
concat(...args: Array<T>): Array<T>; // with adding support of @@isConcatSpreadable and @@species
copyWithin(target: number, start: number, end?: number): Array<T>;
entries(): Iterator<[[index: number], T]>;
every(
callbackfn: (value: T, index: number, target: Array<T>) => boolean,
thisArg?: any
): boolean;
fill(value: T, start?: number, end?: number): Array<T>;
filter(
callbackfn: (value: T, index: number, target: Array<T>) => boolean,
thisArg?: any
): Array<T>; // with adding support of @@species
find(
callbackfn: (value: T, index: number, target: Array<T>) => boolean,
thisArg?: any
): any;
findIndex(
callbackfn: (value: T, index: number, target: Array<T>) => boolean,
thisArg?: any
): number;
findLast(
callbackfn: (value: T, index: number, target: Array<T>) => boolean,
thisArg?: any
): T;
findLastIndex(
callbackfn: (value: T, index: number, target: Array<T>) => boolean,
thisArg?: any
): number;
flat<A, D extends number = 1>(this: A, depth?: D): Array<T>;
flatMap(
mapFn: (value: T, index: number, target: Array<T>) => any,
thisArg: any
): Array<T>;
forEach(
callbackfn: (value: T, index: number, target: Array<T>) => void,
thisArg?: any
): void;
includes(searchElement: T, from?: number): boolean;
indexOf(searchElement: T, from?: number): number;
join(separator?: string): string;
keys(): Iterator<number>;
lastIndexOf(searchElement: any, from?: number): number;
map(
mapFn: (value: T, index: number, target: Array<T>) => any,
thisArg?: any
): Array<T>; // with adding support of @@species
push(...args: Array<T>): number;
reduce(
callbackfn: (memo: any, value: T, index: number, target: Array<T>) => any,
initialValue?: any
): any;
reduceRight(
callbackfn: (memo: any, value: T, index: number, target: Array<T>) => any,
initialValue?: any
): any;
reverse(): Array<T>; // Safari 12.0 bug fix
slice(start?: number, end?: number): Array<T>; // with adding support of @@species
splice(start?: number, deleteCount?: number, ...items: Array<T>): Array<T>; // with adding support of @@species
some(
callbackfn: (value: T, index: number, target: Array<T>) => boolean,
thisArg?: any
): boolean;
sort(comparefn?: (a: any, b: any) => number): Array<T>; // with modern behavior like stable sort
toReversed(): Array<T>;
toSpliced(start?: number, deleteCount?: number, ...items: Array<T>): Array<T>;
toSorted(comparefn?: (a: any, b: any) => number): Array<T>;
unshift(...args: Array<T>): number;
values(): Iterator<T>;
with(index: number, value: T): Array<T>;
[Symbol.iterator]: Iterator<T>;
[Symbol.unscopables]: { [newMethodNames: string]: true };
}
interface ArrayConstructor {
from<T, U>(
items: Iterable<T> | ArrayLike<T>,
mapFn?: (value: T, index: number) => U,
thisArg?: any
): Array<U>;
isArray(value: any): boolean;
of<T>(...args: Array<T>): Array<T>;
}
class Arguments<T> {
[Symbol.iterator](): Iterator<T>; // available only in core-js methods
}
Entry points
core-js(-pure)/es|stable|actual|full/array
core-js(-pure)/es|stable|actual|full/array/from
core-js(-pure)/es|stable|actual|full/array/of
core-js(-pure)/es|stable|actual|full/array/is-array
core-js(-pure)/es|stable|actual|full/array(/virtual)/at
core-js(-pure)/es|stable|actual|full/array(/virtual)/concat
core-js(-pure)/es|stable|actual|full/array(/virtual)/copy-within
core-js(-pure)/es|stable|actual|full/array(/virtual)/entries
core-js(-pure)/es|stable|actual|full/array(/virtual)/every
core-js(-pure)/es|stable|actual|full/array(/virtual)/fill
core-js(-pure)/es|stable|actual|full/array(/virtual)/filter
core-js(-pure)/es|stable|actual|full/array(/virtual)/find
core-js(-pure)/es|stable|actual|full/array(/virtual)/find-index
core-js(-pure)/es|stable|actual|full/array(/virtual)/find-last
core-js(-pure)/es|stable|actual|full/array(/virtual)/find-last-index
core-js(-pure)/es|stable|actual|full/array(/virtual)/flat
core-js(-pure)/es|stable|actual|full/array(/virtual)/flat-map
core-js(-pure)/es|stable|actual|full/array(/virtual)/for-each
core-js(-pure)/es|stable|actual|full/array(/virtual)/includes
core-js(-pure)/es|stable|actual|full/array(/virtual)/index-of
core-js(-pure)/es|stable|actual|full/array(/virtual)/iterator
core-js(-pure)/es|stable|actual|full/array(/virtual)/join
core-js(-pure)/es|stable|actual|full/array(/virtual)/keys
core-js(-pure)/es|stable|actual|full/array(/virtual)/last-index-of
core-js(-pure)/es|stable|actual|full/array(/virtual)/map
core-js(-pure)/es|stable|actual|full/array(/virtual)/push
core-js(-pure)/es|stable|actual|full/array(/virtual)/reduce
core-js(-pure)/es|stable|actual|full/array(/virtual)/reduce-right
core-js(-pure)/es|stable|actual|full/array(/virtual)/reverse
core-js(-pure)/es|stable|actual|full/array(/virtual)/slice
core-js(-pure)/es|stable|actual|full/array(/virtual)/some
core-js(-pure)/es|stable|actual|full/array(/virtual)/sort
core-js(-pure)/es|stable|actual|full/array(/virtual)/splice
core-js(-pure)/es|stable|actual|full/array(/virtual)/to-reversed
core-js(-pure)/es|stable|actual|full/array(/virtual)/to-sorted
core-js(-pure)/es|stable|actual|full/array(/virtual)/to-spliced
core-js(-pure)/es|stable|actual|full/array(/virtual)/unshift
core-js(-pure)/es|stable|actual|full/array(/virtual)/values
core-js(-pure)/es|stable|actual|full/array(/virtual)/with
Example
Array.from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
Array.from({ 0: 1, 1: 2, 2: 3, length: 3 }); // => [1, 2, 3]
Array.from("123", Number); // => [1, 2, 3]
Array.from("123", (it) => it * it); // => [1, 4, 9]
Array.of(1); // => [1]
Array.of(1, 2, 3); // => [1, 2, 3]
let array = ["a", "b", "c"];
for (let value of array) console.log(value); // => 'a', 'b', 'c'
for (let value of array.values()) console.log(value); // => 'a', 'b', 'c'
for (let key of array.keys()) console.log(key); // => 0, 1, 2
for (let [key, value] of array.entries()) {
console.log(key); // => 0, 1, 2
console.log(value); // => 'a', 'b', 'c'
}
function isOdd(value) {
return value % 2;
}
[4, 8, 15, 16, 23, 42].find(isOdd); // => 15
[4, 8, 15, 16, 23, 42].findIndex(isOdd); // => 2
[1, 2, 3, 4].findLast(isOdd); // => 3
[1, 2, 3, 4].findLastIndex(isOdd); // => 2
Array(5).fill(42); // => [42, 42, 42, 42, 42]
[1, 2, 3, 4, 5].copyWithin(0, 3); // => [4, 5, 3, 4, 5]
[1, 2, 3].includes(2); // => true
[1, 2, 3].includes(4); // => false
[1, 2, 3].includes(2, 2); // => false
[NaN].indexOf(NaN); // => -1
[NaN].includes(NaN); // => true
Array(1).indexOf(undefined); // => -1
Array(1).includes(undefined); // => true
[1, [2, 3], [4, 5]].flat(); // => [1, 2, 3, 4, 5]
[1, [2, [3, [4]]], 5].flat(); // => [1, 2, [3, [4]], 5]
[1, [2, [3, [4]]], 5].flat(3); // => [1, 2, 3, 4, 5]
[
{ a: 1, b: 2 },
{ a: 3, b: 4 },
{ a: 5, b: 6 },
].flatMap((it) => [it.a, it.b]); // => [1, 2, 3, 4, 5, 6]
[1, 2, 3].at(1); // => 2
[1, 2, 3].at(-1); // => 3
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]