访问嵌套的不可变映射 属性
Access nested Immutable Map property
我正在学习 Immutable.js。我有一个对象,当被调用时:
myObj.get('people')
returns 以下:
[
{
"name": "John Stevenson",
"country": "Sweden"
},
{
"name": "John Silva",
"country": "Colombia"
},
{
"name": "John Van der Bier",
"country": "Holland"
},
{
"name": "John McDonald",
"country": "Scotland"
}
]
我正试图进入这个对象,所以我只能看到 country
:
myObj.getIn(['people', 'country']) // undefined
我错过了什么?
你的代码的问题是 getIn(['people', 'country'])
的结果试图访问 people
的 country
属性,它是一个数组并且不' 有一个 属性 命名的国家。似乎想要遍历人们并建立他们国家的数组,您可以使用 map
:
var countries = myObj.get('people').map(person => {
return person.country
})
之前的回答会return一个数组。如果你真的想使用 Immutable 你应该使用
import { fromJS } from 'Immutable';
const immutableObj = fromJS(myObj);
//map() or forEach() here
var countries = immutableObj.map(person => {
return person.get('country');
})
我正在学习 Immutable.js。我有一个对象,当被调用时:
myObj.get('people')
returns 以下:
[
{
"name": "John Stevenson",
"country": "Sweden"
},
{
"name": "John Silva",
"country": "Colombia"
},
{
"name": "John Van der Bier",
"country": "Holland"
},
{
"name": "John McDonald",
"country": "Scotland"
}
]
我正试图进入这个对象,所以我只能看到 country
:
myObj.getIn(['people', 'country']) // undefined
我错过了什么?
你的代码的问题是 getIn(['people', 'country'])
的结果试图访问 people
的 country
属性,它是一个数组并且不' 有一个 属性 命名的国家。似乎想要遍历人们并建立他们国家的数组,您可以使用 map
:
var countries = myObj.get('people').map(person => {
return person.country
})
之前的回答会return一个数组。如果你真的想使用 Immutable 你应该使用
import { fromJS } from 'Immutable';
const immutableObj = fromJS(myObj);
//map() or forEach() here
var countries = immutableObj.map(person => {
return person.get('country');
})