Last active
July 17, 2024 10:09
-
-
Save andyj/9626bb130cf8f2fadb84db4913fc2992 to your computer and use it in GitHub Desktop.
Exploring JS array methods
This file contains 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
// This is the code that prompted / created the blog post | |
// https://www.andyjarrett.com/posts/2024/exploring-array-methods-including-push-pop-shift-unshift-map-filter-reduce-and-others/ | |
// push() | |
["š„¶","š„¶","š„¶","š„¶"].push('š„µ') // = ["š„¶","š„¶","š„¶","š„¶","š„µ"] | |
// pop() | |
["š","š„µ","š„¶","š¤¢"].pop() // = ["š","š„µ","š„¶"] | |
// shift() | |
["š","š„µ","š„¶","š¤¢"].shift() // = ["š„µ","š„¶","š¤¢"] | |
// unshift() | |
["š","š„µ","š„¶","š¤¢"].unshift('š©') // = ["š©","š","š„µ","š„¶","š¤¢"] | |
// map() | |
["š","š„µ","š„¶","š¤¢"].map(item => item + '!') // = ["š!","š„µ!","š„¶!","š¤¢!"] | |
// filter() | |
["š","š„µ","š„¶","š¤¢"].filter(item => item === 'š„¶') // = ["š„¶"] | |
// reduce() | |
[1,2,3].reduce((acc, val) => acc + val, 0) // = 6 | |
// some() | |
[1,2,3].some(val => val > 2) // = true | |
// every() | |
[1,2,3].every(val => val > 0) // = true | |
// find() | |
[1,2,3].find(val => val > 2) // = 3 | |
// findIndex() | |
[1,2,3].findIndex(val => val > 2) // = 2 | |
// reverse() | |
["š","š„µ","š„¶","š¤¢"].reverse() // = ["š¤¢","š„¶","š„µ","š"] | |
// at() | |
["š","š„µ","š„¶","š¤¢"].at(1) // = š„µ | |
// slice() | |
["š","š„µ","š„¶","š¤¢"].slice(1, 2) // = ["š„µ"] | |
// concat() | |
["š„¶"].concat(["š„µ"]) // = ["š„¶","š„µ"] | |
// includes() | |
["š","š„µ","š„¶","š¤¢"].includes('š„µ') // = true | |
// indexOf() | |
["š","š„µ","š„¶","š¤¢"].indexOf('š„µ') // = 1 | |
// join() | |
["š","š„µ","š„¶","š¤¢"].join(' ') // = "š š„µ š„¶ š¤¢" | |
// flat() | |
[1,[2,3],[4,5]].flat() // = [1,2,3,4,5] | |
// flatMap() | |
[1,2,3].flatMap(val => [val, val * 2]) // = [1,2,2,4,3,6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment