Created
August 26, 2019 07:36
-
-
Save kartikag01/54d0bd448e9a18f28f02846312c007f6 to your computer and use it in GitHub Desktop.
immutable operations on array in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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