I am trying to add Objects as elements into an array. I am able to restrict the first already added element but subsequent entries are being duplicated.
Here' the code:
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})
}
}
How to stop this duplicate entries? Please suggest. Thanks!