Skip to content

Instantly share code, notes, and snippets.

@jaysoo
Created January 20, 2016 04:40
Show Gist options
  • Save jaysoo/88532049818e0f8dc3c3 to your computer and use it in GitHub Desktop.
Save jaysoo/88532049818e0f8dc3c3 to your computer and use it in GitHub Desktop.
/*
* Immutable data patterns without Immutable.js
*/
// 1. Objects
const a = { name: 'Alice', age: 20, email: 'alice@example.com' };
// updating property
const b = { ...x, age: 21 };
b; //=> { name: 'Alice', age: 21, email: 'alice@example.com' }
// deleting property
const { email: deleted, ...c } = a;
c; //=> { name: 'Alice', age: 20 }
// 2. Arrays
const arr = [1, 2, 3, 4];
// pushing value
const arr2 = x.concat([5]);
arr2; //=> [1, 2, 3, 4, 5]
// unshifting value
const arr3 = [5].concat(arr);
arr3; //=> [5, 1, 2, 3, 4]
// removing from front
const [front, ...arr4] = arr;
arr4; //=> [2, 3, 4]
// removing from back
const arr5 = [].concat(arr).splice(0, 3);
arr5; //=> [1, 2, 3]
// removing from middle (LOL)
const arr6 = [].concat([].concat(arr).splice(0, 1)).concat([].concat(arr).splice(2, 2));
arr6; //=> [1, 3, 4]
@elmigranto
Copy link

.slice(1)
.slice(0, -2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment