Immutable.js Cheat Sheet

From WikiOD

Here is the cheatsheat of Immutable.js and basic guide on how to use Immutable.js

Maps[edit | edit source]

var map = Immutable.Map({ a: 1, b: 2, c: 3 })
map
  .set('b', 50)
  .get('b') // 50

Lists[edit | edit source]

var list = Immutable.List.of(1, 2)

list
  .push(3, 4, 5)
  .unshift(0)
  .concat(list2, list3)
  .get(0)
  .size

Nested maps[edit | edit source]

var nested = Immutable.fromJS({ user: { profile: { name: 'John' } } })

nested
  // Update
  .mergeDeep({ user: { profile: { age: 90 } } })
  .setIn([ 'user', 'profile', 'name' ], 'Jack')
  .updateIn([ 'user', 'profile', 'name' ], (s) => s.toUpperCase())

  // Get
  .getIn(['user', 'profile', 'name']) // 'JACK'

Credit:rstacruz