Repeat structure to replace the value of one array to another

Asked

Viewed 130 times

0

I have the array this.produtoAlvoAlterar.variacao.preco_variacao

I need to replace the variable tag of this array with the value contained in the array this.anuncioAlterar.variations.specifications.preco

I tried to make a repeat structure to pass these values from one array to another, it was like this:

  for(let i=0;i<this.produtoAlvoAlterar.variacao.length;i++){
    for(let j=0;j<this.anuncioAlterar.variations.length;j++){
      for(let k=0;k<this.anuncioAlterar.variations[j].specifications.length;k++){
        if(this.anuncioAlterar.variations[j].specifications[k].key == "Preço"){
          this.produtoAlvoAlterar.variacao[i].preco_variacao = this.anuncioAlterar.variations[j].specifications[k].value;
        }
      }
    }
  }

At the first position of the array the value of this.produtoAlvoAlterar.variacao.preco_variacao should be set to the value "222.22" and in the second position to "333.33" but both positions are having the value "333.33", which is the value of the second key of the array this.anuncioAlterar.variations.specifications

@Edit:

Array this.produtoAlvoAlterar.variacao.preco_variacao

Array this.anuncioAlterar.variations.specifications

  • You can edit your question and put the arrays? so it is easier to help...

  • This data structure of yours is confused. Having 3 Fors inside each other is nothing scalable.

  • I edited the post with the information of the arrays

1 answer

1


Try to use this code. In the code it goes through all this.produtoAlvoAlterar.variacao and this.anuncioAlterar.variations, he gets the Preço of each anuncioAlterar and settar no produtoAlvoAlterar.

this.produtoAlvoAlterar.variacao.forEach(variacao => {
      this.anuncioAlterar.variations.forEach(anuncioVariacao => {
        let novoPreco = anuncioVariacao.specifications.filter(
            chaveValor => {
              return chaveValor.key === 'Preço';
          });

        variacao.preco_variacao = novoPreco;
      })
    });

Browser other questions tagged

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