Skip to content

Instantly share code, notes, and snippets.

@ger86
Created July 8, 2020 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ger86/2e0ffc65015c209a4bc03d5ef4ec3868 to your computer and use it in GitHub Desktop.
Save ger86/2e0ffc65015c209a4bc03d5ef4ec3868 to your computer and use it in GitHub Desktop.
/****************************************************************
*
* 🍰🍰🍰 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