https://array-methods.github.io/
Credits: Axel Rauschmayer
Adding or removing an element at either end of an Array
(return value: item or new array length)
array before | method | return value | array after |
---|---|---|---|
["π¦","π‘","πΊ"] | .push("π©") | 4 | ["π¦","π‘","πΊ","π©"] |
["π¦","π‘","πΊ"] | .pop() | "πΊ" | ["π¦","π‘"] |
["π¦","π‘","πΊ"] | .unshift("π©") | 4 | ["π©","π¦","π‘","πΊ"] |
["π¦","π‘","πΊ"] | .shift() | "π¦" | ["π‘","πΊ"] |
["π¦","π‘","πΊ"] | .unshift(arr.pop()) | 3 | ["πΊ","π¦","π‘"] |
https://array-methods.github.io/
Changing all of an Array
(the input Array is modified and returned)
array before | method | return value |
---|---|---|
["π¦","π‘","πΊ","π©"] | .fill("π‘") | ["π‘","π‘","π‘","π‘"] |
Array(4) | .fill("πΊ") | ["πΊ","πΊ","πΊ","πΊ"] |
Array(4) | .fill("πΊ") .map( (val,idx) => idx ) |
[ 0, 1, 2, 3 ] |
["π¦","π‘","πΊ","π©"] | .reverse() | ["π©","πΊ","π‘","π¦"] |
["c","a","d","b"] | .sort() | ["a","b","c","d"] |
["π¦","π‘","πΊ","π©"] | .sort() | ["πΊ","π‘","π¦","π©"] |
["π¦","π‘","πΊ","π©" ] | .copyWithin(1,2,3) | ["π¦",πΊ","πΊ","π©" ] |
https://array-methods.github.io/
Finding Array elements
array | method | return value |
---|---|---|
["π¦","π‘","πΊ"] | .includes( "π¦" ) | true |
["π¦","π‘","πΊ"] | .indexOf( "π¦" ) | 0 |
["π¦","π‘","π¦"] | .lastIndexOf( "π¦" ) | 2 |
["π¦","π‘","πΊ"] | .find( x => x==="π¦" ) | "π¦" |
["π¦","π‘","πΊ"] | .findIndex( x => x==="π¦" ) | 0 |
https://array-methods.github.io/
Creating a new Array from an existing Array
array before | method (links to MDN) | return value | array after |
---|---|---|---|
["π¦","π‘","πΊ"] | .slice(1, 2) | ["π‘","πΊ"] | ["π¦","π‘","πΊ"] |
["π¦","π‘","πΊ"] | .splice(1, 2) | ["π‘","πΊ"] | ["π¦"] |
["π¦","π‘","π¦"] | .filter( x => x==="π¦") | ["π¦","π¦"] | ["π¦","π‘","π¦"] |
["π¦","π‘"] | .map( x => x+x ) | ["π¦π¦", "π‘π‘"] | ["π¦","π‘"] |
["π¦","π‘"] | .map( x => [x+x] ) | [["π¦π¦"], ["π‘π‘"]] | ["π¦","π‘"] |
["π¦","π‘"] | .flatMap( x => [x,x] ) | ["π¦","π¦","π‘","π‘"] | ["π¦","π‘"] |
["π¦","π‘","πΊ"] | .concat( ["π©","π΄"] ) | ["π¦","π‘","πΊ","π©","π΄"] | ["π¦","π‘","πΊ"] |
https://array-methods.github.io/
Computing a summary of an Array
array | method | return value |
---|---|---|
["π¦","π‘","πΊ"] | .some( x => x==="π‘" ) | true |
["π¦","π‘","πΊ"] | .every( x => x==="π‘" ) | false |
["π¦","π‘","πΊ"] | .join( "π©" ) | "π¦π©π‘π©πΊ" |
[ 2, 3, 4 ] | .reduce( (result,x) => result+x, 10 ) | 19 10+2+3+4 |
["π¦","π‘","πΊ"] | .reduce( (result,x) => result+x,"π©") | "π©π¦π‘πΊ" |
["π¦","π‘","πΊ"] | .reduceRight( (result,x) => result+x,"π©") | "π©πΊπ‘π¦" |
https://array-methods.github.io/
Listing elements
array | method | return value (iterators) |
---|---|---|
["π¦","π‘","πΊ"] | .keys() | [0,1,2] |
["π¦","π‘","πΊ"] | .values() | ["π¦","π‘","πΊ"] |
["π¦","π‘","πΊ"] | .entries() | [ [0,"π¦"], [1,"π‘"], [2,"πΊ"] ] |
spreading ... requiredbecause the above methods return iterators |
return value |
---|---|
[ ...["π¦","π‘","πΊ","π©"].entries() ] Β Β Β Β Β .map( ([key,val]) => val.repeat(key) ) |
["","π‘","πΊπΊ","π©π©π©"] |
Top comments (0)