Answer the question
In order to leave comments, you need to log in
How to remove an element from an array without knowing its index?
Hello.
There is such an example:
<div id="app">
<button v-for="button in arr" @click="active = button">{{button}}</button>
<button @click="remove()">Remove active button</button>
</div>
new Vue({
el: "#app",
data: {
active: null,
arr: ['a', 'b', 'c']
},
methods: {
remove() {
}
}
})
Answer the question
In order to leave comments, you need to log in
If I understand correctly, then:
remove() {
this.arr = this.arr.filter( button => button !== this.active)
}
remove
<button @click="remove()">Remove active button</button>
How do you not know his index?
v-for="(button, index) in arr" and the index immediately appeared
@click="remove(index)"
No need to come up with weird solutions.
But if you need incomprehensible solutions, then based on active, you can make an array filter by the property located in active
computed: {
newArray () {
return this.arr.filter(item => item.id === this.active.id)
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question