I'm facing an issue with comparing two Objects.
I'm comparing an incoming Object, with an Object Array, to see if the Object is already in the array.
I tried two methods:
(betTemps is the Array of Objects, tempBet is the current object which should be compared.)
var duplicate = false;
for (let bet of this.betTemps) {
console.log(tempBet);
console.log(bet);
if(bet === tempBet) {
console.log("reached");
duplicate = true;
break;
}
}
The if afterwards:
.. else if(duplicate){
alert("The Bet is already in the List");
} ...
Console Output after adding an Object which is already in the Array:
Console Output Screenshot
As you can see, they are equal, still the other Object is added to the array.
I tried using this method too with indexOf:
if(this.betTemps.indexOf(tempBet) > -1){
alert("The Bet is already in the List");
}
Console Output after adding an Object which is already in the Array:
Console Output Screenshot
PS:
The code before the comparison:
addBet(bet, index){
var tempBet = Object.assign({}, bet);
var select = (document.getElementById('select'+index)) as HTMLSelectElement;
select = select.options as HTMLSelectElement;
let count = 0;
tempBet.category = [];
for(let i = 0; i < 3; i++) {
if (select[i].selected) {
tempBet.category.push(select[i].value):
count++;
}
}
The code after the comparison:
if (count == 0) {
alert("There has to be at least one Category choosen!");
} else if(duplicate <- This part changes individually which method you use ->){
alert("The Bet is already in the List");
} else {
this.betTemps.push(tempBet);
}