Created
March 21, 2022 10:25
-
-
Save spyesx/c518acf050fd8d3d2a562d027e7dff42 to your computer and use it in GitHub Desktop.
Common array functions in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3].push(4) | |
// [1,2,3,4] | |
[1, 2, 3].pop() | |
// [1,2] | |
[1, 2, 3].shift() | |
// [2,3] | |
[1, 2, 3].unshift(0) | |
// [0,1,2,3] | |
['a', 'b'].concat('c') | |
// ['a','b','c'] | |
['a', 'b', 'c'].join('-') | |
// 'a-b-c' | |
['a', 'b', 'c'].slice(1) | |
// ['b','c'] | |
['a', 'b', 'c'].indexOf('b') | |
// 1 | |
['a', 'b', 'c'].includes('c') | |
// true | |
[3, 5, 6, 8].find((n) => n % 2 === 0) | |
// 6 | |
[2, 4, 3, 5].findIndex((n) => n % 2 !== 0) | |
// 2 | |
[3, 4, 8, 6].map((n) => n * 2) | |
// [6,8,16,12] | |
[1, 4, 7, 8].filter((n) => n % 2 === 0) | |
// [4,8] | |
[2, 4, 3, 7].reduce((acc, curr) => acc + curr) | |
// 16 | |
[2, 3, 4, 5].every((x) => x < 6) | |
// true | |
[3, 5, 6, 8].some((n) => n > 6) | |
// true | |
[1, 2, 3, 4].reverse() | |
// [4,3,2,1] | |
[3, 5, 7, 8].at(-2) | |
// 7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment