Created
July 8, 2020 11:56
-
-
Save ger86/2e0ffc65015c209a4bc03d5ef4ec3868 to your computer and use it in GitHub Desktop.
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
/**************************************************************** | |
* | |
* π°π°π° Array slice π°π°π° | |
* | |
****************************************************************/ | |
const animals = ['πΆ', 'π±', 'π', 'π»', 'π¦']; | |
// Remove first element | |
console.log(animals.slice(1)); | |
// Remove last element | |
console.log(animals.slice(0, -1)); | |
// Shallow clone | |
console.log(animals.slice()); | |
// Get the three first elements | |
console.log(animals.slice(0, 3)); | |
// Get the three last elements | |
console.log(animals.slice(-3)); | |
// And the final trick: delete element at given index | |
function deleteAtIndex(array, index) { | |
return [...array.slice(0,index), ...array.slice(index+1)]; | |
} | |
console.log(deleteAtIndex(animals, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment