Skip to content

Instantly share code, notes, and snippets.

@aboekhoff
Created January 20, 2016 00:04
Show Gist options
  • Save aboekhoff/d468614f27d914777f06 to your computer and use it in GitHub Desktop.
Save aboekhoff/d468614f27d914777f06 to your computer and use it in GitHub Desktop.
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