Repeat structure does not work when my object is copied without reference

Asked

Viewed 34 times

1

I need to remove an attribute from my object (indexvariacaoatributo), but I need it to remain in the root object.

This is my algorithm that receives in variable my object and then I remove the attribute indexvariacaoatributo of the object, in this way works perfectly the algorithm, but in addition to removing the variable variables, it is also removed from the variable categorieForm:

    var variacoes = categoriaForm.variacoes
    if(variacoes[0].estoque_variacao == null || variacoes[0].estoque_variacao == 0){
      variacoes = [];
    }
    //Retira do objeto o indexvariacaoatributo
    for(let i=0;i<variacoes.length;i++){
      for (let j=0;j<variacoes[i].atributo.length;j++){
        console.log(variacoes[i].atributo[j].indexvariacaoatributo);
        delete variacoes[i].atributo[j].indexvariacaoatributo
      }
    }

  console.log(variacoes);

I tried to make an unregistered copy of these forms:

var variacoes = Object.assign({}, categoriaForm.variacoes);

I also tried to:

var variacoes = { ...categoriaForm.variacoes };

But in these two ways, when I print the variable variation I realize that my index is still there. I put on that console.log and I realized it’s not going inside my for.

There is something else that needs to be done when copying an object in this way?

1 answer

1


Man not the cutest way but so it works:

var minhaVar = JSON.parse(JSON.stringify(obj)); 

Browser other questions tagged

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