Javascript 访问列表中的对象
我有一个json-Javascript 访问列表中的对象,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我有一个json- [ { "id": "1", "title": "Mr", "student": { "name": "John" } } ] 我的fetchAPI为- let response = await fetch('https://api....', reqOptions); if (response.status >= 200 && response.status < 300) { con
[
{
"id": "1",
"title": "Mr",
"student": {
"name": "John"
}
}
]
我的fetch
API为-
let response = await fetch('https://api....', reqOptions);
if (response.status >= 200 && response.status < 300) {
const body = await response.json();
this.setState({
studentList: body
});
} else {
let error = response;
throw error;
}
let response=wait fetch('https://api....","选择",;
如果(response.status>=200&&response.status<300){
const body=wait response.json();
这是我的国家({
学生名单:body
});
}否则{
让错误=响应;
投掷误差;
}
我画了地图-
return this.state.studentList.map((student) => {
return (
<View>
<Text>{student.name}</Text>
</View>
)
})
返回此.state.studentList.map((学生)=>{
返回(
{student.name}
)
})
但是
student.name
未定义。我只能访问student.id
或student.title
。如何访问学生对象中的名称?当前您正在尝试访问不在父对象中的属性。name属性属于student属性
您可以尝试从父对象解构学生对象。
即
返回this.state.studentList.map({student})=>{
返回(
{student.name}
)
})
student.student.name
似乎没有说明您最初的示例数据错误或拼写错误。使用student.student.name
时是否有错误?很抱歉,输入错误…现在可以工作了!!!
return this.state.studentList.map(({ student }) => {
return (
<View>
<Text>{student.name}</Text>
</View>
)
})