Reorganize my array’s index

Asked

Viewed 89 times

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.

1 answer

4


See if this fits you. This function removes the elements whose indexvariacaoatributo is equal to the parameter index past and then picks up the elements that have indexvariacaoatributo the front of index removed and decreases 1.

var array = [
    {indexvariacaoatributo: 0, id: 5, tipovariacao: "Cor", valorvariacao: "Azul"},
    {indexvariacaoatributo: 0, id: 7, tipovariacao: "Tamanho", valorvariacao: "P"},
    {indexvariacaoatributo: 1, id: 6, tipovariacao: "Cor", valorvariacao: "Amarelo"},
    {indexvariacaoatributo: 2, id: 7, tipovariacao: "Tamanho", valorvariacao: "P"}];

function remover(index, array) {

    let arrayElemRemovido = array.sort((v1, v2) => v1.indexvariacaoatributo > v2.indexvariacaoatributo)
                                 .filter(e => e.indexvariacaoatributo != index)
                                 .map(e => { if (e.indexvariacaoatributo > index) {
                                               e.indexvariacaoatributo -= 1; 
                                             }

                                             return e; 
                                     });
    // Reorganiza os índices para que o primeiro elemento do array seja sempre zero
    while(arrayElemRemovido.length > 0 && arrayElemRemovido.filter(v => v.indexvariacaoatributo == 0).length == 0) {
        arrayElemRemovido.map(v => { v.indexvariacaoatributo -= 1; return v });
    }

    return arrayElemRemovido;

}

var array = remover(0, array);
console.log(array);

/* Saída: 
    0: {indexvariacaoatributo: 0, id: 6, tipovariacao: "Cor", valorvariacao: "Amarelo"}
    1: {indexvariacaoatributo: 1, id: 7, tipovariacao: "Tamanho", valorvariacao: "P"}
*/

var array = remover(1, array);
console.log(array);

/* Saída: 
    0: {indexvariacaoatributo: 0, id: 6, tipovariacao: "Cor", valorvariacao: "Amarelo"}
*/

var array = [
    {indexvariacaoatributo: 1, id: 5, tipovariacao: "Cor", valorvariacao: "Azul"},
    {indexvariacaoatributo: 2, id: 7, tipovariacao: "Tamanho", valorvariacao: "P"},
    {indexvariacaoatributo: 1, id: 6, tipovariacao: "Cor", valorvariacao: "Amarelo"},
    {indexvariacaoatributo: 2, id: 7, tipovariacao: "Tamanho", valorvariacao: "P"}];

var array = remover(2, array);
console.log(array);

/* Saída: 
    0: {indexvariacaoatributo: 0, id: 5, tipovariacao: "Cor", valorvariacao: "Azul"}
    1: {indexvariacaoatributo: 0, id: 6, tipovariacao: "Cor", valorvariacao: "Amarelo"}
*/
  • 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

  • I had to make some adjustments but your logic was correct, managed to help me reach my goal. thanks.

  • 1

    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.

  • yes, it worked with the adjustments, the reward of 300 reputation is available in 24h

  • 1

    Thanks friend and good luck on your project.

  • 1

    @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.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.