Created
July 4, 2021 16:45
-
-
Save EmmanuelBeziat/92a143e2ef0479c4d3e703298be36999 to your computer and use it in GitHub Desktop.
JS Arrays Cheatsheet
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
/** 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