Skip to content

Instantly share code, notes, and snippets.

@gn-bcampbell
Created June 19, 2023 07:40
Show Gist options
  • Save gn-bcampbell/52301bd4c96a38e8e844b697002bc6ce to your computer and use it in GitHub Desktop.
Save gn-bcampbell/52301bd4c96a38e8e844b697002bc6ce to your computer and use it in GitHub Desktop.
Selecting the correct JS Array Function
let arr = [1,2,3,4,5,6,7]
// IF I WANT...
// ---- Mutate the original array ---- //
// Add to
arr.push() // end
arr.unshift() //start
// Remove from
arr.pop() //end
arr.shift() //start
arr.splice() //any index
//Others
arr.reverse()
arr.sort()
arr.fill();
// ---- Create a new Array ---- //
arr.map() // from the original
arr.filter() // with a condition
arr.slice() // portion of original
arr.concat() //adding original to other
arr.flat() //flatten the original
arr.flatMap()
// ---- An array index ---- //
arr.indexOf() // based on value
arr.findIndex() //based on test condition
// ---- An array element ---- //
arr.find() // based on test condition
// ---- Know if array includes ---- //
arr.includes() //based on value
arr.some() //based on test condition
// ---- A new string ---- //
arr.join() // based on separator string
// ---- To transform to value ---- //
arr.reduce() // boil down to single value
// ---- To just loop array ---- //
arr.forEach() // based on callback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment