Last active
September 21, 2021 14:12
-
-
Save AllBitsEqual/5c96f988fe7220017f290e43b1f85028 to your computer and use it in GitHub Desktop.
A short rundown of JavaScript Array methods, visualised using Emoji because people love emoji... [π¦π©]
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
/* | |
* some helpers to keep the examples clean | |
*/ | |
const makeUnicorn = (animal) => animal === 'π΄' ? 'π¦' : 'π©' | |
const isUnicorn = (animal) => animal === 'π¦' | |
const isNoUnicorn = (animal) => animal !== 'π¦' | |
const countUnicorns = (count, animal) => count + (animal === 'π¦' ? 1 : 0) | |
const copyAnimals = (animals) => [...animals] | |
/* | |
* THE GOOD GUYS | |
* these methods return a new array or result WITHOUT MUTATION of the original array | |
*/ | |
// map() - return anything for every element in the array | |
console.log(['π΄', 'π΄', 'π', 'π΄'].map(makeUnicorn)) // ['π¦', 'π¦', 'π©', 'π¦'] | |
// filter() - return only elements that meet some condition | |
console.log(['π΄', 'π΄', 'π¦', 'π΄'].filter(isUnicorn) // ['π¦'] | |
console.log(['π΄', 'π΄', 'π¦', 'π΄'].filter(isNoUnicorn)) // ['π΄', 'π΄', 'π΄'] | |
// find() - return the first element meeting some condition | |
console.log(['π¦', 'π¦', 'π', 'π'].find(isNoUnicorn)) // 'π' | |
// findIndex() - return index of first element meeting some condition | |
console.log(['π¦', 'π¦', 'π', 'π'].findIndex(isNoUnicorn)) // 2 | |
// some() - returns true if ANY of the elements meet the condition | |
console.log(['π΄', 'π΄', 'π¦', 'π΄'].some(isUnicorn)) // true | |
// every() - returns true if ALL of the elements meet the condition | |
console.log(['π΄', 'π΄', 'π¦', 'π΄'].every(isUnicorn)) // false | |
// reduce() - an accumulator... read the MDN => https://bit.ly/MDN_reduce | |
console.log(['π¦', 'π΄', 'π¦', 'π΄'].reduce(countUnicorns, 0)) // 2 | |
/* | |
* WHEN PUSH COMES TO SHOVE | |
* how to add to and take from a stack WITH MUTATION of the original array | |
*/ | |
const animals = ['π΄', 'π', 'π'] | |
// push() - add at the end / returns new length after mutation | |
console.log(animals.push('π¦')) // 4 | |
console.log(animals) // ['π΄', 'π', 'π', 'π¦'] | |
// unshift() - add at the start / returns new length after mutation | |
console.log(animals.unshift('π')) // 5 | |
console.log(animals) // ['π', 'π΄', 'π', 'π', 'π¦'] | |
// pop() - removes last / returns removed element | |
console.log(animals.pop()) // 'π¦' | |
console.log(animals) // ['π', 'π΄', 'π', 'π'] | |
// shift() - removes first / returns removed element | |
console.log(animals.shift()) // 'π' | |
console.log(animals) // ['π΄', 'π', 'π'] | |
/* | |
* SLICE AND DICE, BUT NICE! | |
* get parts from an array WITHOUT MUTATION of the original array | |
*/ | |
const herd = ['π΄', 'π΄', 'π', 'π', 'π', 'π', 'π΄'] | |
console.log(herd.slice(3)) // ['π', 'π', 'π', 'π΄'] | |
console.log(herd.slice(3, 4)) // ['π'] | |
console.log(herd.slice(0, 3)) // ['π΄', 'π΄', 'π'] | |
console.log(herd.slice(-3)) // ['π', 'π', 'π΄'] | |
console.log(herd.slice(3, -1)) // ['π', 'π', 'π'] | |
console.log(herd) // ['π΄', 'π΄', 'π', 'π', 'π', 'π', 'π΄'] | |
/* | |
* THE BAD BOY! | |
* splice allows you to mutate an array by adding, removing | |
* or replacing elements at any one point in the array | |
*/ | |
// remove everything starting at position | |
const herd1 = copyAnimals(herd) | |
console.log(herd1) // ['π΄', 'π΄', 'π', 'π', 'π', 'π', 'π΄'] | |
console.log(herd1.splice(3)) // ['π', 'π', 'π', 'π΄'] | |
console.log(herd1) // ['π΄', 'π΄', 'π'] | |
// remove element(s) at position | |
const herd2 = copyAnimals(herd) | |
console.log(herd2) // ['π΄', 'π΄', 'π', 'π', 'π', 'π', 'π΄'] | |
console.log(herd2.splice(3, 1)) // ['π'] | |
console.log(herd2) // ['π΄', 'π΄', 'π', 'π', 'π', 'π΄'] | |
// add neew element(s) at position | |
const herd3 = copyAnimals(herd) | |
console.log(herd3) // ['π΄', 'π΄', 'π', 'π', 'π', 'π', 'π΄'] | |
console.log(herd3.splice(3, 0, 'π¦')) // [] | |
console.log(herd3) // ['π΄', 'π΄', 'π', 'π¦', 'π', 'π', 'π', 'π΄'] | |
// replace element(s) at position with new element(s) | |
const herd4 = copyAnimals(herd) | |
console.log(herd4) // ['π΄', 'π΄', 'π', 'π', 'π', 'π', 'π΄'] | |
console.log(herd4.splice(3, 1, 'π¦', 'π¦')) // ['π'] | |
console.log(herd4) // ['π΄', 'π΄', 'π', 'π¦', 'π¦', 'π', 'π', 'π΄'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment