marcandrewb
7/10/2016 - 5:04 PM

Javascript Immutable Operations

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}