1
I have two cards in my app.
The first card has index 0, the second card has index 1.
There is a variable called listAtributos
which 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 card index and the index index, through which my cards show the items.
Example:
In card 0, because it has index 0, it will be shown through ngfor the following items:
0: {indexvariacaoatributo: 0, id: 5, tipovariacao: "Cor", valorvariacao: "Azul"}
1: {indexvariacaoatributo: 0, id: 7, tipovariacao: "Tamanho", valorvariacao: "P"}
In card 1, because it has index 1, it will be shown through my ngfor, the following items:
2: {indexvariacaoatributo: 1, id: 6, tipovariacao: "Cor", valorvariacao: "Amarelo"}
3: {indexvariacaoatributo: 1, id: 7, tipovariacao: "Tamanho", valorvariacao: "P"}
At a certain point in my application I need to remove one of these cards, and that’s when the index of my listAtribute variable gets lost.
I tried to make an algorithm so that when a card is deleted, the indexvariacaoatributo is updated to appear on the remaining card.
Example:
If I delete card 0, the indexvariacaoatributo of the 1 must be 0.
If I delete card 1, the indexvariacaoatributo of those who were 0 should become 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 index variable is received as a parameter of this function, it is the index of the card to be deleted.
You are giving: Argument of type '(v1: any, v2: any) => Boolean' is not Assignable to Parameter of type '(a: any, b: any) => number'. Type 'Boolean' is not Assignable to type 'number'. at the line of the Sort function
– veroneseComS
I had to make some adjustments but your logic was correct, managed to help me reach my goal. thanks.
– veroneseComS
This code I posted is not Typescript, it’s Javascript. So maybe it needs a few adaptations since the syntax is very similar. However, you can copy this entire code in the Chrome Console and run through there you will see the algorithm working. Then you adjust to the TS.
– Hélio Márcio Filho
yes, it worked with the adjustments, the reward of 300 reputation is available in 24h
– veroneseComS
Thanks friend and good luck on your project.
– Hélio Márcio Filho
@Renaotpls, then edit Helium’s answer with the adjustments you made, so the answer is worth 100% to Typescript, which was the original question, and can help other people who are looking for something similar.
– Pedro Gaspar