【发布时间】:2019-09-06 09:49:56
【问题描述】:
我正在使用复选框中选中的对象创建新数组。但是当我再次提交时,出现错误“flattenChildren(...):遇到两个具有相同密钥的孩子,.$3。子密钥必须是唯一的;当两个孩子共享一个密钥时,只会使用第一个孩子。 "我只想推送独特的对象。
我正在使用反应
handleSubmit(){
let students = []
for(let idx in this.state.checked){
if(this.state.checked[idx] ){
students.push(this.state.allStudent[idx])
}
}
console.log('students', students)
this.setState({
studentList: update(this.state.studentList, {$push: students})
})
}
【问题讨论】:
-
对不起,我是新手。感谢您的帮助!
-
你不必为不知道的事情感到抱歉。至于这个问题,就在你推送到状态之前..做一个查找,如果你找到跳过推送...a Array.findIndex((i)=>{ return children.id === currentchild.id})你应该很高兴
-
考虑使用 Set 而不是 Array :他们的
add方法对于给定参数是幂等的 -
将
students.push(this.state.allStudent[idx])更改为!students.includes(this.state.allStudent[idx]) && students.push(this.state.allStudent[idx])
标签: javascript arrays reactjs push