Legend:
- βοΈ method changes
this. - π method does not change
this.
Array<T>.prototype.*:
-
concat(...items: Array<T[] | T>): T[]π ES3Returns a new array that is the concatenation of
thisand allitems. Non-array parameters are treated as if they were arrays with single elements.> ['a'].concat('b', ['c', 'd']) [ 'a', 'b', 'c', 'd' ] -
copyWithin(target: number, start: number, end=this.length): thisβοΈ ES6Copies the elements whose indices range from
startto (excl.)endto indices starting withtarget. Overlapping is handled correctly.> ['a', 'b', 'c', 'd'].copyWithin(0, 2, 4) [ 'c', 'd', 'c', 'd' ] -
entries(): Iterable<[number, T]>π ES6Returns an iterable over [index, element] pairs.
> Array.from(['a', 'b'].entries()) [ [ 0, 'a' ], [ 1, 'b' ] ] -
every(callback: (value: T, index: number, array: Array<T>) => boolean, thisArg?: any): booleanπ ES5Returns
trueifcallbackreturnstruefor every element. Stops as soon as it receivesfalse. Math: β> [1, 2, 3].every(x => x > 0) true > [1, -2, 3].every(x => x > 0) false -
fill(value: T, start=0, end=this.length): thisβοΈ ES6Assigns
valueto every index.> [0, 1, 2].fill('a') [ 'a', 'a', 'a' ] -
filter(callback: (value: T, index: number, array: Array<T>) => any, thisArg?: any): T[]π ES5Returns an array with only those elements for which
callbackreturnstrue.> [1, -2, 3].filter(x => x > 0) [ 1, 3 ] -
find(predicate: (value: T, index: number, obj: T[]) => boolean, thisArg?: any): T | undefinedπ ES6The result is the first element for which
predicatereturnstrue. If it never does, the result isundefined.> [1, -2, 3].find(x => x < 0) -2 > [1, 2, 3].find(x => x < 0) undefined -
findIndex(predicate: (value: T, index: number, obj: T[]) => boolean, thisArg?: any): numberπ ES6The result is the index of the first element for which
predicatereturnstrue. If it never does, the result is-1.> [1, -2, 3].findIndex(x => x < 0) 1 > [1, 2, 3].findIndex(x => x < 0) -1 -
forEach(callback: (value: T, index: number, array: Array<T>) => void, thisArg?: any): voidπ ES5Calls
callbackfor each element.['a', 'b'].forEach((x, i) => console.log(x, i)) // 'a' 0 // 'b' 1
-
includes(searchElement: T, fromIndex=0): booleanπ ES2016Returns
trueifsearchElementSameValueZero-equal to an element andfalse, otherwise. SameValueZero-equal means: strictly equal, butNaNis also equal to itself.> [0, 1, 2].includes(1) true > [0, 1, 2].includes(5) false -
indexOf(searchElement: T, fromIndex=0): numberπ ES5Returns the index of the first element that is strictly equal to
searchElement. Returns-1if there is no such element. Starts searching at indexfromIndex, visiting subsequent indices next.> ['a', 'b', 'a'].indexOf('a') 0 > ['a', 'b', 'a'].indexOf('a', 1) 2 > ['a', 'b', 'a'].indexOf('c') -1 -
join(separator = ','): stringπ ES1Creates a string by concatenating string representations of all elements, separating by
separator.> ['a', 'b', 'c'].join() 'a,b,c' > ['a', 'b', 'c'].join('##') 'a##b##c' -
keys(): Iterable<number>π ES6Returns an iterable over the keys of the array.
> [...['a', 'b'].keys()] [ 0, 1 ] -
lastIndexOf(searchElement: T, fromIndex=this.length-1): numberπ ES5Returns the index of the last element that is strictly equal to
searchElement. Returns-1if there is no such element. Starts searching at indexfromIndex, visiting preceding indices next.> ['a', 'b', 'a'].lastIndexOf('a') 2 > ['a', 'b', 'a'].lastIndexOf('a', 1) 0 > ['a', 'b', 'a'].lastIndexOf('c') -1 -
map<U>(callback: (value: T, index: number, array: ReadonlyArray<T>) => U, thisArg?: any): U[]π ES5Returns a new array, in which every element is the result of
callbackbeing applied to the corresponding element ofthis.> [1, 2, 3].map(x => x * 2) [ 2, 4, 6 ] > ['a', 'b', 'c'].map((x, i) => i) [ 0, 1, 2 ] -
pop(): T | undefinedβοΈ ES3Removes and returns the last element of the array. That is, it treats the end of the array as a stack.
> const arr = ['a', 'b', 'c']; > arr.pop() 'c' > arr [ 'a', 'b' ] -
push(...items: T[]): numberβοΈ ES3Adds adds zero or more
itemsto the end of the array. That is, it treats the end of the array as a stack. The return value is the length ofthisafter the change.> const arr = ['a', 'b']; > arr.push('c', 'd') 4 > arr [ 'a', 'b', 'c', 'd' ] -
reduce<U>(callback: (state: U, element: T, index: number, array: T[]) => U, firstState?: U): Uπ ES5The
callbackcomputes the next state, given the current state and anelementof the array..reduce()feeds it the array elements, starting at index 0, going forward. If nofirstStateis provided, the array element at index 0 is used, instead. The last state is the result of.reduce().> [1, 2, 3].reduce((state, x) => state + String(x), '') '123' > [1, 2, 3].reduce((state, x) => state + x, 0) 6 -
reduceRight<U>(callback: (state: U, element: T, index: number, array: T[]) => U, firstState?: U): Uπ ES5Works like
.reduce(), but visits the array elements backward, starting with the last element.> [1, 2, 3].reduceRight((state, x) => state + String(x), '') '321' -
reverse(): thisβοΈ ES1Rearranges the elements of the array so that they are in reverse order and then returns
this.> const arr = ['a', 'b', 'c']; > arr.reverse() [ 'c', 'b', 'a' ] > arr [ 'c', 'b', 'a' ] -
shift(): T | undefinedβοΈ ES3Removes and returns the first element of the array. The opposite of
.unshift().> const arr = ['a', 'b', 'c']; > arr.shift() 'a' > arr [ 'b', 'c' ] -
slice(start=0, end=this.length): T[]π ES3Returns a new array, with the elements of
thiswhose indices are between (incl.)startand (excl.)end.> ['a', 'b', 'c', 'd'].slice(1, 3) [ 'b', 'c' ] > ['a', 'b'].slice() // shallow copy [ 'a', 'b' ] -
some(callback: (value: T, index: number, array: Array<T>) => boolean, thisArg?: any): booleanπ ES5Returns
trueifcallbackreturnstruefor at least one element. Stops as soon as it receivestrue. Math: β> [1, 2, 3].some(x => x < 0) false > [1, -2, 3].some(x => x < 0) true -
sort(compareFn?: (a: T, b: T) => number): thisβοΈ ES1Sorts the array and returns
this. The order in which to sort is specified viacompareFn, which returns a number that is:- Negative if
a < b(mnemonic: negative = less than zero) - Zero if
a === b - Positive if
a > b
> [3, 1, 2].sort((a, b) => a - b) [ 1, 2, 3 ] > ['b', 'a', 'c'].sort((a, b) => a < b ? -1 : a > b ? +1 : 0) [ 'a', 'b', 'c' ] - Negative if
-
splice(start: number, deleteCount=this.length-start, ...items: T[]): T[]βοΈ ES3At index
start, it removesdeleteCountelements and inserts theitems. It returns the deleted elements.> const arr = ['a', 'b', 'c', 'd']; > arr.splice(1, 2, 'x', 'y') [ 'b', 'c' ] > arr [ 'a', 'x', 'y', 'd' ] -
toString(): stringπ ES1Returns a string with string versions of all elements, separated by commas.
> [1, 2, 3].toString() '1,2,3' > ['a', 'b', 'c'].toString() 'a,b,c' > [].toString() '' -
unshift(...items: T[]): numberβοΈ ES3Inserts the
itemsat the beginning of this array and returns the length ofthisafter the modification.> const arr = ['c', 'd']; > arr.unshift('e', 'f') 4 > arr [ 'e', 'f', 'c', 'd' ]
- Array methods of various ECMAScript versions in detail: http://exploringjs.com
- Holes and array methods: Sect. βArray operations and holesβ in βExploring ES6β.
- https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es6.d.ts
- MDN
- ECMAScript spec