Monday, May 20, 2024
73
rated 0 times [  75] [ 2]  / answers: 1 / hits: 28723  / 7 Years ago, thu, april 20, 2017, 12:00:00

Unfortunately the documentation is very sparse :



https://facebook.github.io/immutable-js/docs/#/Map/getIn



Does anyone have an example? I am guessing that if I have a myObject like so :



  a: {
b:{
c:banana
}
}


that




myObject.getIn([a, b, c])




will return banana.



However the immutable objects can also be map objects which leaves me thoroughly confused.


More From » immutable.js

 Answers
38

Shortly:



map.getIn([a, b, c]) is a shortcut to map.get(a).get(b).get(c)



In details:



You have probably got into one of the fromJS traps. Calling :



const map = Immutable.fromJS({a: {b: {c: banana}}});


creates a Map with only key a, which's value is also a Map with only key b, which's value is also a Map with only key c and value banana.



With other words fromJS goes into the deep of the object provided, and defines Map for each Object and a List for each Array



With this example, calling map.getIn([a, b, c]) is kind of a shortcut to map.get(a).get(b).get(c)



But if you define map as a Map:



const map = new Immutable.Map({a: {b: {c: banana}}});


it creates a Map, with only key a, which's value is plain object {b: {c: banana}}, and calling a map.get(a).get(b).get(c) will throw you something like get is not a function, since map.get(a) will return {b: ...} plain object.



Same reasons are why map.getIn([a, b, c]) will not work as you might expect.


[#58082] Tuesday, April 18, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dahlias

Total Points: 730
Total Questions: 104
Total Answers: 101

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;