Skip to content

Instantly share code, notes, and snippets.

@raphaelkross
Created March 27, 2016 05:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raphaelkross/64f16df55dc9c1dce38c to your computer and use it in GitHub Desktop.
Save raphaelkross/64f16df55dc9c1dce38c to your computer and use it in GitHub Desktop.
Javascript Immutable Operations
// 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