Skip to content

Instantly share code, notes, and snippets.

@MarcoWorms
Forked from rauschma/api-cheatsheet-array.md
Last active April 2, 2018 20:39
Show Gist options
  • Save MarcoWorms/fd6e9a9a92739d464146ad21fe80f7d2 to your computer and use it in GitHub Desktop.
Save MarcoWorms/fd6e9a9a92739d464146ad21fe80f7d2 to your computer and use it in GitHub Desktop.

JS native immutable array methods

Reduced from https://gist.github.com/rauschma/f7b96b8b7274f2e2d8dab899803346c3

Methods

concat(...items: (T | Array<T>)[]): T[] ES3

Returns a new array that is the concatenation of this and all items. Non-array parameters are treated as if they were arrays with single elements.

> ['a'].concat('b', ['c', 'd'])
[ 'a', 'b', 'c', 'd' ]

entries(): Iterable<[number, T]> ES6

Returns 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 ES5

Returns true if callback returns true for every element. Stops as soon as it receives false. Math: ∀

> [1, 2, 3].every(x => x > 0)
true
> [1, -2, 3].every(x => x > 0)
false

filter(callback: (value: T, index: number, array: Array<T>) => any, thisArg?: any): T[] ES5

Returns an array with only those elements for which callback returns true.

> [1, -2, 3].filter(x => x > 0)
[ 1, 3 ]

find(predicate: (value: T, index: number, obj: T[]) => boolean, thisArg?: any): T | undefined ES6

The result is the first element for which predicate returns true. If it never does, the result is undefined.

> [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 ES6

The result is the index of the first element for which predicate returns true. 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 ES5

Calls callback for each element.

['a', 'b'].forEach((x, i) => console.log(x, i))
// 'a' 0
// 'b' 1

includes(searchElement: T, fromIndex=0): boolean ES2016

Returns true if searchElement is an element and false, otherwise.

> [0, 1, 2].includes(1)
true
> [0, 1, 2].includes(5)
false

indexOf(searchElement: T, fromIndex=0): number ES5

Returns the index of the first element that is strictly equal to searchElement. Returns -1 if there is no such element. Starts searching at index fromIndex, 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 ES1

Creates 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> ES6

Returns an iterable over the keys of the array.

> [...['a', 'b'].keys()]
[ 0, 1 ]

lastIndexOf(searchElement: T, fromIndex=this.length-1): number ES5

Returns the index of the first element that is strictly equal to searchElement. Returns -1 if there is no such element. Starts searching at index fromIndex, 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[] ES5

Returns a new array, in which every element is the result of callback being applied to the corresponding element of this.

> [1, 2, 3].map(x => x * 2)
[ 2, 4, 6 ]
> ['a', 'b', 'c'].map((x, i) => i)
[ 0, 1, 2 ]

reduce<U>(callback: (state: U, element: T, index: number, array: T[]) => U, firstState?: U): U ES5

The callback computes the next state, given the current state and an element of the array. .reduce() feeds it the array elements, starting at index 0, going forward. If no firstState is 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 ES5

Works like .reduce(), but visits the array elements backward, starting with the last element.

> [1, 2, 3].reduceRight((state, x) => state + String(x), '')
'321'

slice(start=0, end=this.length): T[] ES3

Returns a new array, with the elements of this whose indices are between (incl.) start and (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 ES5

Returns true if callback returns true for at least one element. Stops as soon as it receives true. Math: ∃

> [1, 2, 3].some(x => x < 0)
false
> [1, -2, 3].some(x => x < 0)
true

toString(): string ES1

Returns 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()
''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment