Skip to content

Instantly share code, notes, and snippets.

@ancyrweb
Last active January 12, 2022 07:07
Show Gist options
  • Save ancyrweb/56f4e0ffc09a33128afb6ffae4e1c964 to your computer and use it in GitHub Desktop.
Save ancyrweb/56f4e0ffc09a33128afb6ffae4e1c964 to your computer and use it in GitHub Desktop.
const elements = [1, 2, 4, 5];
const indexToInsert = 2; // we will append the element at index 2
const elementToInsert = 3; // we will insert the number "3"
const nextElements = [
...elements.slice(0, indexToInsert),
elementToInsert,
...elements.slice(indexToInsert),
]; // [1, 2, 3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment