Skip to content

Instantly share code, notes, and snippets.

@ericsoco
Created August 24, 2018 05:38
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 ericsoco/72538faae3587fe117c5f094248bb79c to your computer and use it in GitHub Desktop.
Save ericsoco/72538faae3587fe117c5f094248bb79c to your computer and use it in GitHub Desktop.
Immutable splice (Javascript / ES6)
const merge = (arr, val, i) => [
...arr.slice(0, i),
val,
...arr.slice(i + 1)
];
const arr1 = ['a', 'b'];
console.log(merge(arr1, 'foo', 1));
console.log(merge(arr1, 'foo', 0));
console.log(merge(arr1, 'foo', 2));
console.log(arr1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment