How to save an object (JSON) by copying in Javascript

Asked

Viewed 688 times

1

How to store the current state of a json object per copy? Because if I save by reference I lose the exact information of the object at that moment of assignment. Object example:

var sessao = {"num":"1"};  var vetorSessoes = [];

function salvando(){
  var copia = sessao; 
  var vetorSessoes.push(copia);
  sessao.num = 2;
}

function teste(){
  console.log(vetorSessoes[0]); //deveria printar '1' ao invés de '2'
}

For vectors, I learned to use the .slice(), how do I make this copy with objects and specifically json.

  • 1

    It is important to understand that in this example, sessao is not a JSON. He is a object. JSON is a data representation that uses the Javascript object format.

2 answers

3

For a single copy (Shallow) of an object can use Object.assign:

var copia = Object.assign({}, sessao);

See how it already gives the result you expect:

var sessao = {"num":"1"};  
var vetorSessoes = [];

function salvando(){
  var copia = Object.assign({}, sessao);
  vetorSessoes.push(copia);
  sessao.num = 2;
}

function teste(){
  console.log("Copia tem ", vetorSessoes[0]); //deveria printar '1' ao invés de '2'
}

salvando();
teste();
console.log("Sessão tem ", sessao);

However this will not work properly if you have objects inside objects, for example:

var sessao = { 
    "num":"1",
    "casa": {
        "area": 110,
        "divisoes": 5
    }
};

In this case you have to make a deep copy (deep), which you can do using JSON.parse and JSON.stringify:

var copia = JSON.parse(JSON.stringify(sessao));

Example also of this version:

var sessao = { 
  "num":"1",
  "casa": {
    "area": 110,
    "divisoes": 5
  }
};    
var vetorSessoes = [];

function salvando(){
  var copia = JSON.parse(JSON.stringify(sessao));
  vetorSessoes.push(copia);
  sessao.num = 2;
  sessao.casa.area = 90;
  sessao.casa.divisoes = 3;
}

function teste(){
  console.log("Copia tem ", vetorSessoes[0]); //deveria printar '1' ao invés de '2'
}

salvando();
teste();
console.log("Sessão tem ", sessao);

0

Missed you include in sessao the value of num if you just want to include the value in the array, which is what it looks like when printing vetorSessoes[0]:

var copia = sessao.num;

var sessao = {"num":"1"};
var vetorSessoes = [];

function salvando(){
  var copia = sessao.num; 
  vetorSessoes.push(copia);
  sessao.num = 2;
  teste();
}

function teste(){
  console.log(vetorSessoes[0]); //deveria printar '1' ao invés de '2'
}
<button type="button" onclick="salvando()">Salvar</button>

Or you can add the object as JSON to the array:

var sessao = {"num":"1"};
var vetorSessoes = [];

function salvando(){
  var copia = JSON.stringify(sessao);
  vetorSessoes.push(copia);
  sessao.num = 2;
  teste();
}

function teste(){
  console.log(JSON.parse(vetorSessoes[0]).num); //deveria printar '1' ao invés de '2'
}
<button type="button" onclick="salvando()">Salvar</button>

Browser other questions tagged

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