Created
January 20, 2016 00:04
-
-
Save aboekhoff/d468614f27d914777f06 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
const h1 = { | |
a: { | |
b: { | |
c: { | |
foo: "bar" | |
} | |
} | |
} | |
} | |
// we want to change foo to "baz" | |
const h2 = Object.assign({}, h1, { | |
a: Object.assign({}, h1.a, { | |
b: Object.assign({}, h1.b, { | |
c: Object.assign({}, h1.c, { | |
foo: "baz" | |
}) | |
}) | |
}) | |
}) | |
// vs | |
const m1 = Immutable.Map(h1) | |
const m2 = m1.setIn(['a', 'b', 'c'], "baz"); | |
// and similarly with arrays | |
// let's say we want to increment h1.a.b.c[2] | |
const h1 = { | |
a: { | |
b: { | |
c: [1, 2, 3, 4, 5] | |
} | |
} | |
} | |
const a2 = h2.a.b.c.slice(); | |
a2[2] += 1; | |
const h2 = Object.assign({}, h1, { | |
a: Object.assign({}, h1.a, { | |
b: Object.assign({}, h1.b, { | |
c: a2 | |
}) | |
}) | |
}) | |
// vs | |
const m1 = Immutable.Map(h2) | |
const m2 = m1.updateIn(['a', 'b', 'c', 2], (n) => { return n + 1 }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment