Skip to content

Instantly share code, notes, and snippets.

@kartikag01
Created August 26, 2019 07:36
Show Gist options
  • Save kartikag01/54d0bd448e9a18f28f02846312c007f6 to your computer and use it in GitHub Desktop.
Save kartikag01/54d0bd448e9a18f28f02846312c007f6 to your computer and use it in GitHub Desktop.
immutable operations on array in javascript
const a = [1,2,3];
// Remove Elements from index
const index = 1;
const b = [ ...a.slice(0, index), ...a.slice(index + 1, a.length), ];
console.log('a',a);
console.log('b',b);
// Add Element to index
const index2 = 1;
const val = 5;
const b2 = [ ...a.slice(0, index2), val ,...a.slice(index2, a.length), ];
console.log('a',a);
console.log('b',b2);
console.log('', b2.slice(0, 0));
// console.log([1,2,3].slice(1,3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment