Created
April 12, 2022 00:08
-
-
Save arielivandiaz/4f8e760d9aee7e427008e14f60a97372 to your computer and use it in GitHub Desktop.
JS-ES6 Cheatsheet Arrays
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
//Arrays elements could be with different types | |
let user = ["Ariel", 24, "Diaz"]; | |
let [name, age, lastname] = user; | |
var arr = ["Pear", "Apple", "Orange"]; | |
//CHECK IF IS AN ARRAY | |
console.log(Array.isArray(arr)); | |
//CREATE FROM | |
//Static method creates a new, shallow-copied Array instance from an array-like or iterable object | |
console.log(Array.from(arr[0])); | |
// PUSH | |
//Adds one or more elements to the end of an array and returns the new length of the array | |
console.log("Array new length : ", arr.push("Mango")); | |
// POP | |
//Removes the last element from an array and returns that element | |
console.log("Deleted : ", arr.pop()); | |
// UNSHIFT | |
//Adds one or more elements to the beginning of an array and returns the new length of the array | |
console.log("Array new length : ", arr.unshift("Strawberry", "Cherry")); | |
// SHIFT | |
//Removes the first element from an array and returns that removed element | |
console.log("Deleted : ", arr.shift()); | |
//REMOVE DUPLICATE VALUES USING SET | |
//Returns a new array | |
console.log([...new Set(arr)]); | |
console.log(Array.from(new Set(arr))); //Same operation | |
//SPLICE | |
//Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. | |
console.log(arr); | |
arr.splice(1, 0, "Peach"); //Add peach at position 1 | |
console.log(arr); | |
arr.splice(4, 1); //Remove one element at position 4 | |
console.log(arr); | |
arr.splice(4, 1, "Watermelon"); //Replace element at position 4 | |
console.log(arr); | |
// SLICE | |
//Returns a shallow copy of a portion of an array into a new array object selected from start to end | |
console.log(arr.slice(3)); //Starting in position 3 | |
console.log(arr.slice(1, 3)); //Values between position 1 and 3. | |
console.log(arr.slice(-2)); //Last 2 values. | |
//FIND | |
//Returns the first element in the provided array that satisfies the provided testing function | |
console.log(arr.find(a => a === "Apple") ? "Yes Apple" : "No Apple"); | |
// INCLUDES | |
//Return a boolean if whether an array includes a certain value among its entries | |
console.log(arr.includes("Apple") ? "Found" : "Not found"); | |
//FIND INDEX | |
//Returns the index of the first element in the array that satisfies the provided testing function | |
let appleIndex = arr.findIndex(a => a === "Apple"); | |
console.log(appleIndex !== -1 ? `Apple is at ${appleIndex}` : "No Apple"); | |
//INDEX OF | |
//Returns the first index at which a given element can be found in the array | |
let orangeIndex = arr.indexOf("Orange", 2); //Starting at 2 position | |
console.log(orangeIndex !== -1 ? `Orange is at ${orangeIndex}` : "No Orange"); | |
//LAST INDEX OF | |
//Returns the last index at which a given element can be found in the array | |
arr.push('Orange'); | |
let orangeLastIndex = arr.lastIndexOf("Orange"); | |
console.log(orangeLastIndex !== -1 ? `Orange is at ${orangeLastIndex}` : "No Orange"); | |
//SOME | |
//Tests whether at least one element in the array passes the test implemented by the provided function | |
console.log(arr.some(a => a === 'Orange')); | |
//TO STRING | |
//Returns a string representing the specified array and its elements | |
console.log(arr.toString()); | |
//TO LOCALESTRING | |
//Returns a string representing the elements of the array. The elements are converted to Strings using their toLocaleString | |
console.log(arr.toLocaleString()); | |
//JOIN | |
//Returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string | |
console.log(arr.join(" - ")); | |
//CONCAT | |
//Returns a new array merge two or more arrays | |
let newArr = arr.concat(["Blackberry", "Kiwi"], ['Pineapple']); | |
console.log(newArr); | |
//FLAT | |
//Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth | |
arr.push(["Blackberry", "Kiwi"]); | |
console.log(arr.flat()); | |
arr = arr.flat(); | |
//flat any deep | |
const ARRAY_DEEP = Infinity; | |
arr.flat(ARRAY_DEEP); | |
//FILTER | |
//Creates a new array with all elements that pass the test implemented by the provided function | |
console.log(arr.filter(a => a.length < 6)); | |
//EVERY | |
//Tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value | |
console.log(arr.every(a => a.length > 3)); | |
//SORT | |
//Sorts the elements of an array in place and returns the sorted array, by default sort order is ascending. | |
console.log(arr.sort()); | |
console.log(arr.sort((a, b) => a > b ? -1 : 1)); | |
//REVERSE | |
//The first array element becomes the last, and the last array element becomes the first. | |
console.log(arr.reverse()); | |
//FOR EACH | |
//Executes a provided function once for each array element | |
arr.forEach(e => console.log(e)); | |
//MAP | |
//Creates a new array populated with the results of calling a provided function on every element in the calling array. | |
let obj = arr.map(e => { | |
return { | |
name: e, | |
quantity: Math.floor(Math.random() * (10)) | |
} | |
}); | |
console.log(obj); | |
//REDUCE | |
//executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element | |
const total = obj.reduce((accumulator, currentValue) => | |
accumulator + currentValue.quantity, 0 //Accumulator started in 0 | |
); | |
console.log(total); | |
//REDUCE RIGTH | |
//Applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value. | |
const word = obj.reduceRight((accumulator, currentValue) => | |
accumulator + currentValue.name[0], '' //Accumulator started in 0 | |
); | |
console.log(word); | |
//TODO | |
//flatMap() | |
//fill() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment