I am trying to check if an object is within an array, I have managed to do this however I am unable to run the code within the blocks. Here is the code snippet:
basket.add = function(item) {
for(var i = 0; i < this.items.length; i++){
if(item === this.items[i]){
basket.items[i].count += 1;
}else{
basket.items.push(item);
};
};
basket.print();
};
When I run the code without the if statement it works fine apart from the fact it push the obj again. When I check and then push the obj if it is not in the array this code does not work.
itemandthis.items, you are using===so it checks to see if variables are equal and the same type before returning result. We need more info