【发布时间】:2018-10-28 23:15:05
【问题描述】:
如何将新对象推送到数组中?每当我在 react 中运行此代码时,它都会不断替换数组中的第一个对象。
radioButtonVal = (e) => {
this.radioid = e.target.name;
this.radiovalue = e.target.value;
this.radioButtonValTitles = [];
this.radioButtonValFind = this.radioButtonValTitles.find(x => x.id === this.radioid);
if(this.radioButtonValFind){
this.radioButtonValFind.id = this.radioid;
this.radioButtonValFind.value = this.radiovalue;
} else {
this.radioButtonValTitles.push({id: this.radioid, value: this.radiovalue})
}
}
输出是 ({object, object}) 只是替换当前值
预期的输出是 ({object, object}, {object, object}...)
【问题讨论】:
-
你能创建一个minimal reproducible example吗?
-
每次调用该函数时都会分配一个空数组..
-
谢谢,我没看到
标签: javascript reactjs