Function that runs for and exits according to condition

0

I have an array called listAttributes: any [] = []; and I need to remove the rows that match my index.

If I give a console log this way, it shows me exactly which elements should be excluded:

removeAtributo(index: number){
       for(let i=0;i<this.listAtributos.length;i++){
          if(this.listAtributos[i].indexvariacaoatributo == index){
            console.log(this.listAtributos[i]) 
          }
      }
}

But when I change to the splice function, only the first element is deleted, having to double-click the button that calls this exclude function.

Should not you delete the two since it's in a repeat structure?

With the splice:

removeAtributo(index:number){
       for(let i=0;i<this.listAtributos.length;i++){
          if(this.listAtributos[i].indexvariacaoatributo == index){
            this.listAtributos.splice(i,1)  
          }
     }
}

Example input:

My listAttributes object has:

0: {indexvariacaoatributo: 0, id: 5, tipovariacao: "Cor", valorvariacao: "Azul"}
1: {indexvariacaoatributo: 0, id: 6, tipovariacao: "Cor", valorvariacao: "Amarelo"}
2: {indexvariacaoatributo: 1, id: 5, tipovariacao: "Cor", valorvariacao: "Azul"}
3: {indexvariacaoatributo: 1, id: 7, tipovariacao: "Tamanho", valorvariacao: "P"}

I need to remove the rows where the indexvariance attribute equals the index passed in the function removeAttribute ()

If my index passing in parameter is 1, you must remove:

2: {indexvariacaoatributo: 1, id: 5, tipovariacao: "Cor", valorvariacao: "Azul"}
3: {indexvariacaoatributo: 1, id: 7, tipovariacao: "Tamanho", valorvariacao: "P"}
    
asked by anonymous 08.11.2018 / 20:30

2 answers

1

What happens is as follows. In the given example your array has 4 items.

When you start the for, it will try to go through the 4 items. However, after it removes the first element that enters its criterion (index 2), the last element of (index 3) becomes index 2, since it takes the place of the element removed and its array decreases in size (goes from 4 elements to 3).

With this, your variable i goes to 3 and the size of your array also and the loop closes (due to i < this.listAtributos.length condition).

The ideal would be a solution of the type:

removeAtributo(index:number){
   for(let i=0;i<this.listAtributos.length;i++){
      if(this.listAtributos[i].indexvariacaoatributo == index){
        this.listAtributos.splice(i,1);
        i--; //evita que ele ignore o próximo elemento
      }
   }
}
    
08.11.2018 / 20:56
0

Since you are doing a FOR by changing a global attribute, when you fetch an item from the index and use splice to remove it, the next item you just deleted ends up in the place of the newly added item removed, example:

1: { item 1 }
2: { item 2 }
3: { item 3 }

When you remove item 2, which has index 2 , item 3 that has index 3 will take the place of item 2, then it will have index 2 .

1: { item 1 }
2: { item 3 }

Since you are using typescript in Angular, a much simpler solution would be to use the filter method:

removeAtributo(index : number){
    this.listAtributos = this.listAtributos.filter(
     (atributos) => atributos.indexvariacaoatributo != index )
}

Read more in Documentation Array.prototype.filter ()

    
08.11.2018 / 21:34