I have two cards in my application.
The first card has index 0, the second card has index 1.
There is a variable called listAtributos
that has the following structure:
0: {indexvariacaoatributo: 0, id: 5, tipovariacao: "Cor", valorvariacao: "Azul"}
1: {indexvariacaoatributo: 0, id: 7, tipovariacao: "Tamanho", valorvariacao: "P"}
2: {indexvariacaoatributo: 1, id: 6, tipovariacao: "Cor", valorvariacao: "Amarelo"}
3: {indexvariacaoatributo: 1, id: 7, tipovariacao: "Tamanho", valorvariacao: "P"}
There is a relationship between the index of the card and the indexvariance attribute, through it the items are shown on my cards.
Example:
On card 0, by having index 0, the following items will be shown through ngfor:
0: {indexvariacaoatributo: 0, id: 5, tipovariacao: "Cor", valorvariacao: "Azul"}
1: {indexvariacaoatributo: 0, id: 7, tipovariacao: "Tamanho", valorvariacao: "P"}
In card 1, by having index 1, the following items will be shown through my ngfor:
2: {indexvariacaoatributo: 1, id: 6, tipovariacao: "Cor", valorvariacao: "Amarelo"}
3: {indexvariacaoatributo: 1, id: 7, tipovariacao: "Tamanho", valorvariacao: "P"}
At some point in my application I need to remove one of these cards, and that's when the indexes of my variable listAtributes get lost.
I tried to make an algorithm so that when a card is deleted, the attribute index is updated to appear on the card left over.
Example:
If I delete card 0, the indexvariance attribute of those that were 1 should be 0.
If I delete card 1, the indexvariance attribute of those that were 0 should be 1.
I tried something like:
for(let i=0;i<this.listAtributos.length;i++){
if(this.listAtributos[i].indexvariacaoatributo == index){
this.listAtributos[i].indexvariacaoatributo = index - 1;
}
}
The variable index is received as a parameter of this function, it is the index of the card to be excluded.