gpt4 book ai didi

javascript - 从状态映射数组内的数组

转载 作者:行者123 更新时间:2023-11-28 12:52:48 24 4
gpt4 key购买 nike

所以我的代码是:

class MyComponent extends Component {
constructor(props) {
super(props);

this.state = {

myArray: [
{id:1, continent:[
{id:1, name: 'Russia'},
{id:2, name: 'Germany'},
{id:3, name: 'Poland'}
]},

{id:2, continent:[
{id:1, name: 'China'},
{id:2, name: 'India'},
{id:3, name: 'Bangladesh'}
]},

{id:3, continent:[
{id:1, name: 'Brazil'},
{id:2, name: 'Peru'},
{id:3, name: 'Chile'}
]}
],

otherValue: 23
}
}


subBoardList(id){
return this.state.myArray[id].map((continent) => {
return(
<View>
<Text>{continent.name}</Text>
</View>
)
})
}


render() {
return (
{this.subBoardList(2)}
);
}
}

我想显示 id 传递给 subBoardList 函数的大陆数组的内容。我该怎么做?

错误:

TypeError: undefined is not an object (evaluating 'this.state.myArray[id].map')

最佳答案

您应该使用:

this.state.myArray[id].continent.map(...

关于javascript - 从状态映射数组内的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59446268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com