Skip to content

Instantly share code, notes, and snippets.

@EmmanuelBeziat
Created July 4, 2021 16:45
Show Gist options
  • Save EmmanuelBeziat/92a143e2ef0479c4d3e703298be36999 to your computer and use it in GitHub Desktop.
Save EmmanuelBeziat/92a143e2ef0479c4d3e703298be36999 to your computer and use it in GitHub Desktop.
JS Arrays Cheatsheet
/** Static properties **/
// Creates an array from a String
// Output ['🍎', '🍌', 'πŸ‡']
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/from
Array.from('πŸŽπŸŒπŸ‡')
// Check for an array
// Output true
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
Array.isArray(['🍎', '🍌', 'πŸ‡'])
// Creates a new Array with provided elements
// Output ['🍎', '🍌', 'πŸ‡']
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/of
Array.of('🍎', '🍌', 'πŸ‡')
/** Instance properties */
// Joins two arrays
// Output ['🍎', '🍌', 'πŸ‡', 'πŸ“']
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
['🍎', '🍌'].concat(['πŸ‡', 'πŸ“'])
// Copy first two array elements to last two
// Output: ['🍎', '🍌', '🍎', '🍌']
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin
['🍎', '🍌', 'πŸ‡', 'πŸ“'].copyWithin(2, 0)
// Returns the array that matches the test
// Output ['🍎']
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
['🍎', '🍌', 'πŸ‡'].filter(emoji => emoji === '🍎')
// Get the index of '🍌'
// Output 1
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
['🍎', '🍌', 'πŸ‡'].indexOf('🍌')
// Returns the value of the first element that satisfies function 🍌
// Output: '🍌'
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/find
['🍎', '🍌', 'πŸ‡'].find(emoji => emoji === '🍌')
// Returns the index of the first element that satisfies function 🍌
// Output: 1
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
['🍎', '🍌', 'πŸ‡'].findIndex(emoji => emoji === '🍌')
// Create a new array with all elements of nested arrays
// Ouput ['🍎', '🍌', 'πŸ‡', 'πŸ“']
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
['🍎', '🍌', ['πŸ‡', 'πŸ“']].flat()
// Executes a provided function once for each array element
// Output πŸŽπŸŒπŸ‡
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
['🍎', '🍌', 'πŸ‡'].forEach(emoji => console.log(emoji))
// Creates a new array by calling a function for each array element
// Output πŸŽπŸŒπŸ‡
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/map
['🍎', '🍌', 'πŸ‡'].map(emoji => console.log(emoji))
// Checks if every element in the array has a value 🍎
// Output false
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/every
['🍎', '🍌', 'πŸ‡'].every(emoji => emoji === '🍎')
// Checks if at least one element in the array has a value 🍎
// Output true
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/some
['🍎', '🍌', 'πŸ‡'].some(emoji => emoji === '🍎')
// Checks if the array contains 'πŸ“'
// Output false
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
['🍎', '🍌', 'πŸ‡'].includes('πŸ“')
// Joins all elements of an array into a string
// Output "🍎 - 🍌 - πŸ‡"
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/join
['🍎', '🍌', 'πŸ‡'].join(' - ')
// Removes and return the last element of an array
// Output 'πŸ‡'
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/pop
['🍎', '🍌', 'πŸ‡'].pop()
// Adds new elements to the end of an array and returns length
// Output 4
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/push
['🍎', '🍌', 'πŸ‡'].push('πŸ’')
// Reverses the order of the elements in the array
// Output ['πŸ‡', '🍌', '🍎']
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
['🍎', '🍌', 'πŸ‡'].reverse()
// Adds/Removes elements
// Output (removed array) ['🍌', 'πŸ‡'], (new array) ['🍎']
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
['🍎', '🍌', 'πŸ‡'].splice(1, 2)
// Selects a part of an array and returns the new array
// Output ['🍌']
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
['🍎', '🍌', 'πŸ‡'].slice(1, 2)
// Converts an array to a string, and returns the result
// Output '🍎,🍌,πŸ‡'
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
['🍎', '🍌', 'πŸ‡'].toString()
// Remove the first element of an array, and return that element
// Output '🍎'
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
['🍎', '🍌', 'πŸ‡'].shift()
// Add new elements to the beginning and returns the new length
// Output 4
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
['🍎', '🍌', 'πŸ‡'].unshift('🍐')
// Reduce the values of an array to a single value
// Output 'πŸŒπŸŽπŸŒπŸ‡'
// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
['🍎', '🍌', 'πŸ‡'].reduce((acc, cur) => acc + cur, '🍌')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment