I have a list stored as data in my VueJS component which is rendered to the DOM by a v-for loop that iterates over the items in the array. I have a button that pops an item off the array and I'm trying to make the add button add back 1 item at a time if that item is not already in the array.
addItem: function(){
for (item in this.array){
if (item !== item){
this.array.push(item)
}else{
continue
}
}
},
答案 0 :(得分:0)
Your if statement doesn't really make sense.
Your code doesn't really show where the item you want to add to the array comes from. Maybe it should be passed as an function argument.
Anyway, if you want to add item to an array under the condition that the item isn't already in the array you can use Array.prototype.includes.
if (!this.array.includes(item)) this.array.push(item)