Skip to content
Advertisement

How to delete many data in array if condition vue js

I have select many data and post to back end, it is succeed but after that i want to delete array data in front end without reload. I try with splice it is only delete one or two max is tow

Here is my code

const itemOrder = this.baskets.filter((el)=>{
    return el.checkbox == true
});

HTTP().post('/order',itemOrder).then(()=>{
});

after then i wont to delete all this baskets if checkbox is true

i have try

for(let i in this.baskets) {
    if(this.baskets[i].checkbox == true) {
        console.log(i);
        this.baskets.splice(i,1);
    }
}

and also try this

this.baskets.splice(this.baskets.findIndex(e => e.checkbox == true),1);

still does not work as my expectation

Advertisement

Answer

You can filter all false checkboxes:

let baskets = [
  {id: 1, checkbox: true},
  {id: 2, checkbox: true},
  {id: 3, checkbox: false},
  {id: 4, checkbox: true},
  {id: 5, checkbox: true}
]

baskets = baskets.filter(b =>  b.checkbox === false)

console.log(baskets)
Advertisement