我正在尝试将对象作为元素添加到数组中。我能够限制已添加的第一个元素,但是后续的条目会被重复添加。
这是代码:
onAddButtonPress(data, id, name){
const items = this.props.items;
if(items.length >= 1){
items.forEach(i=>
{
if(i.id !== id){
const arr = data.map(i=>{
return i.name
})
this.props.addToShopList({id:id, arr:arr, name:name})
}
}
)
}
else{
const arr = data.map(i=>{
return i.name
})
this.props.addToShopList({id:id, arr:arr, name:name})
}
}
如何停止重复记录? 请给出建议。谢谢!