Created
March 27, 2016 05:18
-
-
Save raphaelkross/64f16df55dc9c1dce38c to your computer and use it in GitHub Desktop.
Javascript Immutable Operations
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
// Arrays & Objs Without Mutations | |
var list = ['1', '2', '3']; | |
var obj = {item: 1, blob: 2}; | |
// Adds item to array | |
list.concat(['4', '5']); | |
[...list, [4, 5]]; | |
// Remove | |
list.slice(0, index).concat( list.slice(index + 1) ); | |
[...list.slice(0, index), | |
...list.slice(index + 1)] | |
// Update | |
[...list.slice(0, index), | |
list[index] + 1, | |
...list.slice(index + 1)] | |
// Object | |
Object.assign({}, objSource, { | |
changed: newVal | |
}); | |
{...obj, | |
completed: newVal} | |
Or use seamless-immutable lib > https://github.com/rtfeldman/seamless-immutable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment