rtfeldman / seamless-immutable

Immutable data structures for JavaScript which are backwards-compatible with normal JS Arrays and Objects.
BSD 3-Clause "New" or "Revised" License
5.37k stars 195 forks source link

How to use Immutable toolset? #197

Open fedorinoGore opened 7 years ago

fedorinoGore commented 7 years ago

Hi. Im just started to use this library and a little bit confused. At first look it has some powerfull tools - like replace, without, etc. But while trying to work with a real data model - I just cant figured out how to use those utilities for data model manipulations. For example. Very common task, I believe. Have a store with object bundles in it. Looks like

bundles: {
count: 12,
items: [ {
  name: 'some name',
  id: '213123123',
  field: 'sdfdsf',
  children: []
}, {}, {} ...]
}

So I want to change some of the properties in one of the bundle.items array. How to do this smooth and beautifull using Immutable tooling without calling lodash or underscore for help?

LukeusMaximus commented 7 years ago

To perform a modification you can use setIn() to achieve this:

bundles = Immutable.setIn(bundles, ["items", 0, "name"], "new name")

will give:

bundles: {
count: 12,
items: [ {
  name: 'new name',
  id: '213123123',
  field: 'sdfdsf',
  children: []
}, {}, {} ...]
}