我正在尝试将数据插入一个空的 JSON 数组,但遇到了问题。我在构造函数中定义数组,然后在页面加载时向后端发出几个 get 请求,在获得响应后,我想将新的数组元素添加到现有元素中。这是我正在使用的代码:
constructor() {
super();
this.state = {
sds: []
}
}
componentDidMount() {
axios.get('/userData', {
params: {
user: this.props.auth.user.name
}
}).then(res => {
for(var i=0; i<res.data[0].chemID.split(',').length; i++){
if(res.data[0].chemID.split(',')[i] != 0){
axios.get('/chemData', {
params: {
id: res.data[0].chemID.split(',')[i]
}
//This is where I want to insert the data
}).then(res => this.sds += ({
id: i,
title: res.data[0].chemName,
selected: false,
key: 'sds'
}))
}
}
})
}
扬帆大鱼
慕标5832272
相关分类