I have an array of objects and I add new objects to it if the user make some choices. Then, when I refresh the page, I make sure I save these options, to keep showing them unless the user deletes them.
At the same time, if the user tries to add the same option once again, the code behind won´t let them do it.
The problem is that, after refreshing, if I try to add the same option again, the array does not recognize the existing item as the same as the one I try to add and it allows me to do so.
Actually, here is an example of an item and what I have in the array when that happens: item:
{name: "Vinos", type_products: "Vinos", active: true}
active: true
name: "Vinos"
type_products: "Vinos"
[[Prototype]]: Object
Array: filtersSelected.
this.filtersSelected[0]
{name: "Vinos", type_products: "Vinos", active: true}
active: true
name: "Vinos"
type_products: "Vinos"
[[Prototype]]: Object
So, as you see, it is the same thing, however, when I do any of these verifications, I always received the wrong value:
const index = this.filtersSelected.indexOf(item); //the value is always -1
this.filtersSelected.filter(el => el == item) //the value is always [] (an empty array)
this.filtersSelected.includes(item) //the value is always false
How can I check / compare the values between what I have in the array and my item, to do some things, only, if the item is not already in the array?
(like
if(index <= -1){array.push(item);}
)
Thanks a lot!
itemis an object not a value, so those methods won't work. You can't use comparisons like that on an object. You'll have to iterate over the object properties to see if they match the conditions.