Rico Letterman Rico Letterman - 1 month ago 15
Javascript Question

Access nested Immutable Map property

I'm learning Immutable.js. I have an object that when called:

myObj.get('people')


returns the following:

[
{
"name": "John Stevenson",
"country": "Sweden"
},
{
"name": "John Silva",
"country": "Colombia"
},
{
"name": "John Van der Bier",
"country": "Holland"
},
{
"name": "John McDonald",
"country": "Scotland"
}
]


I'm trying to get inside this object so I can only see
country
:

myObj.getIn(['people', 'country']) // undefined


What am I missing?

Answer

The problem with you're code is that the result of getIn(['people', 'country']) is attempting to access the country property of people, which is an array and doesn't have a property named country. It seems like want to loop over people and build an array of their countries, which you can do with map:

var countries = myObj.get('people').map(person => {
    return person.country
})