Skip to content

Instantly share code, notes, and snippets.

@gp22
Created December 10, 2017 22:21
Show Gist options
  • Save gp22/08172c7002f17f8d3be8b175d4f76bc8 to your computer and use it in GitHub Desktop.
Save gp22/08172c7002f17f8d3be8b175d4f76bc8 to your computer and use it in GitHub Desktop.
Array snippets in JS
// Use slice and reduce to create a new array with new elements inserted
[array[0], ...array.slice(1).reduce((arr, existingElement) => [...arr, elementToInsert, existingElement], [])]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment