我正在尝试将数据插入到空的JSON数组中,但遇到了麻烦。我在构造函数中定义数组,然后在页面加载时向后端发送一些请求,并在获得响应后,我想将新数组元素添加到现有数组。这是我正在使用的代码:
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'
}))
}
}
})
}RISEBY
杨__羊羊
相关分类