Arrays 如何获取嵌套的Json并使其成为React中的对象数组
我正在设置一个列表数组,并希望将值从JSON解析到该列表 这是数组Arrays 如何获取嵌套的Json并使其成为React中的对象数组,arrays,json,reactjs,Arrays,Json,Reactjs,我正在设置一个列表数组,并希望将值从JSON解析到该列表 这是数组 const universityOptions = [ { key: '1', text: 'Universtiy 1', value: 'Universtiy 1' }, { key: '2', text: 'Universtiy 2', value: 'Universtiy 2' }, { key: '3', text: 'Universtiy 3', value: 'Universtiy 3' }, { k
const universityOptions = [
{ key: '1', text: 'Universtiy 1', value: 'Universtiy 1' },
{ key: '2', text: 'Universtiy 2', value: 'Universtiy 2' },
{ key: '3', text: 'Universtiy 3', value: 'Universtiy 3' },
{ key: '4', text: 'Universtiy 4', value: 'Universtiy 4' },
{ key: '5', text: 'Universtiy 5', value: 'Universtiy 5' },
{ key: '6', text: 'Universtiy 6', value: 'Universtiy 6' }
]
下面是json
{"success":true,"data":[{"id":1,"name":"University 1"},{"id":2,"name":"University 2"},{"id":3,"name":"University 3"},{"id":4,"name":"University 4"},{"id":5,"name":"University 5"},{"id":6,"name":"University 6"}]}
这是我到目前为止尝试过的方法,我得到了数据,但我只需要data.name(大学名称),我一直在想如何得到它
componentDidMount() {
const univFetch = fetch('url')
// university_list state
univFetch.then(res => {
if( res.status === 200)
return res.json()
}).then( univJson => {
this.setState({
university_list: univJson.data
})
console.log(univJson.data);
})
}
结果
(6) [{…}, {…}, {…}, {…}, {…}, {…}]
0: {id: 1, name: "University 1"}
1: {id: 2, name: "University 2"}
2: {id: 3, name: "University 3"}
3: {id: 4, name: "University 4"}
4: {id: 5, name: "University 5"}
5: {id: 6, name: "University 6"}
length: 6
__proto__: Array(0)
我希望输出是一个类似于
const universityOptions = [
{ key: '1', text: 'Universtiy 1', value: 'Universtiy 1' },
{ key: '2', text: 'Universtiy 2', value: 'Universtiy 2' },
{ key: '3', text: 'Universtiy 3', value: 'Universtiy 3' },
{ key: '4', text: 'Universtiy 4', value: 'Universtiy 4' },
{ key: '5', text: 'Universtiy 5', value: 'Universtiy 5' },
{ key: '6', text: 'Universtiy 6', value: 'Universtiy 6' }
]
谢谢您需要迭代您的响应,您可以像这样创建所需的输出
this.setState({
university_list : univJson.data.map(data => ({key:data.id,text:data.name,value:data.name}))
}, ()=>console.log(this.state.university_list))
错误是在univJson上使用
forEach并创建数组
componentDidMount() {
const univFetch = fetch('url')
// university_list state
univFetch.then(res => {
if( res.status === 200)
return res.json()
}).then( univJson => {
let univArray = [];
univJson.forEach((datum, index) => {
univArray.push({ key: datum.id, text: datum.name, value: datum.name});
})
this.setState({
university_list: univArray
})
console.log(univJson.data);
})
}
试着这样做:
const newArray = json.data.map(elem => ({
key: elem.id.toString(),
text: elem.name,
value: elem.name
}));
componentDidMount() {
const univFetch = fetch('url')
univFetch.then(res => {
if( res.status === 200)
return res.json()
}).then( univJson => {
const universityList = univJson.data.map(elem => ({
key: elem.id.toString(),
text: elem.name,
value: elem.name
}));
this.setState({
university_list: universityList
})
})
}
您的componentDidMount()
最终会是这样的:
const newArray = json.data.map(elem => ({
key: elem.id.toString(),
text: elem.name,
value: elem.name
}));
componentDidMount() {
const univFetch = fetch('url')
univFetch.then(res => {
if( res.status === 200)
return res.json()
}).then( univJson => {
const universityList = univJson.data.map(elem => ({
key: elem.id.toString(),
text: elem.name,
value: elem.name
}));
this.setState({
university_list: universityList
})
})
}
如果你想看看的话。希望有帮助。为什么不做一个额外的手术
componentDidMount() {
const univFetch = fetch('url')
// university_list state
univFetch.then(res => {
if( res.status === 200)
return res.json()
}).then( univJson => {
var output = [];
for(var i=0 ;i<univJson.data;i++){
var obj = {key : univJson.data[i]["id"],
text : univJson.data[i]["name"],
value : univJson.data[i]["name"]
}
output.push(obj)
}
this.setState({
university_list: output
});
console.log(output);
})
}
componentDidMount(){
const univFetch=fetch('url')
//州立大学
然后(res=>{
如果(资源状态===200)
return res.json()
})。然后(univJson=>{
var输出=[];
对于(var i=0;iYou必须映射您的响应数据。Muchas gracias Mate!De nada!很高兴它有帮助