Reorganize index from my array

1

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.

    
asked by anonymous 09.11.2018 / 16:06

1 answer

4

See if that meets you. This function removes elements whose indexvariation attribute is equal to the last index parameter, and then takes the elements that have indexvariance attribute index removed and decrements 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"}
*/
    
09.11.2018 / 17:25