What does getIn () do in Immutable.js?
Unfortunately, the documentation is very sparse:
https://facebook.github.io/immutable-js/docs/#/Map/getIn
Does anyone have an example? I guess if I have myObject like:
a: {
b:{
c:"banana"
}
}
what
myObject.getIn (["a", "b", "c"])
will return the banana.
However, immutable objects can also be map objects, which leaves me completely at a loss.
source to share
In the near future :
map.getIn(["a", "b", "c"])
is a shortcut to map.get("a").get("b").get("c")
More details:
You probably fell into one of the traps fromJS
. Call:
const map = Immutable.fromJS({a: {b: {c: "banana"}}});
creates Map
with only key a
, whose value is also equal Map
with only key b
, whose value is also equal Map
with only key c
and value banana
.
In other words, fromJS
goes into the depth of the provided object and defines Map
for each Object
and a List
for eachArray
In this example, the call map.getIn(["a", "b", "c"])
is a shortcut tomap.get("a").get("b").get("c")
But if you define Map
as Map
:
const map = new Immutable.Map({a: {b: {c: "banana"}}});
it creates a map with a single key a
whose value is a simple object {b: {c: "banana"}}
, and the call will map.get("a").get("b").get("c")
throw you something like get is not a function
it map.get("a")
will return a {b: ...}
simple object.
For the same reason why map.getIn(["a", "b", "c"])
won't work the way you might expect.
source to share