我正在嘗試將數(shù)據(jù)插入到空的JSON數(shù)組中,但遇到了麻煩。我在構(gòu)造函數(shù)中定義數(shù)組,然后在頁面加載時向后端發(fā)送一些請求,并在獲得響應(yīng)后,我想將新數(shù)組元素添加到現(xiàn)有數(shù)組。這是我正在使用的代碼: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'
}))
}
}
})
}
將新元素插入空J(rèn)SON對象
慕碼人8056858
2019-04-18 17:15:24