Created
January 20, 2016 04:40
-
-
Save jaysoo/88532049818e0f8dc3c3 to your computer and use it in GitHub Desktop.
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
/* | |
* 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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.slice(1)
.slice(0, -2)