I have a React function, where I want to add an object to a javascript array. So far I have this
selectAnimalHandler = (animal) =>{
console.log(animal.id)
if(this.state.animals.find(ani => ani !== animal )){
this.setState(prevState =>(
{selectedAnimals: [...prevState.selectedAnimals, animal]}))
}
}
the logic is to check rather an element already exist in an array, and if it does not exist add it to the array. I try to make a condition, rather the element exists in the array or not. if it does not exist, nothing happens