Confusions with JSON.stringify, JSON.parse, and OBJECT.push ()

1

Well I'm messing with these commands, in reality what I'm wanting to do is the following. Using localStorage maintain a database.

NOME TABELA | DADOS TABELA
tbl_TESTE   | {teste 01, teste02, teste03, ...}
tbl_TESTE02 | {arroz, feijão, batata, ...}

**Sendo que todos os dados da tabela é um array de OBJETOS.

When I start the application I'm setting the banks as nulls I do this:

localStorage.setItem("tbl_TABELA01", []);
localStorage.setItem("tbl_TABELA02", []);
localStorage.setItem("tbl_TAVELA03", []);

This is done only once so as not to reset the already entered data.

After inserting I do this as follows:

var nome = $("#lblNome").val(),
    telefone = $("#lblTelefone").val();

var dados = [];

   dados.lbNome = nome;
   dados.lbTelefone = telefone;

   console.log(dados);

//var result = JSON.stringify(dados);

// console.log(result);

var tbl_CELULAS = localStorage.getItem("tbl_CELULAS");

   console.log(tbl_CELULAS);

// var parsetbl_CELULAS = JSON.parse(tbl_CELULAS);

// console.log(parsetbl_CELULAS);

     tbl_CELULAS.push(dados);

console.log(tbl_CELULAS);

//var JSON = JSON.stringify(tbl_CELULAS);

  console.log(JSON);

This is a mess because I am not sure how to set up the logic in order to solve the problem. What I need is before inserting, get what I already have in the siteStorage, thus inserting in the last position what I just searched, then last save again in the localStorage.

    
asked by anonymous 05.05.2016 / 19:57

1 answer

3

LocalStorage stores strings. When you want to reset you must pass strings to the setter, ie instead of localStorage.setItem("tbl_TABELA01", []); you must have localStorage.setItem("tbl_TABELA01", '[]');

If you want to be sure of the order of the recording you have to use arrays. Using Objects, and Object.keys() , does not return keys in order of insertion.

When you do var tbl_CELULAS = localStorage.getItem("tbl_CELULAS"); this returns you a string and not a pointer that you can change by reference. In the localStorage you have to read everything (in string) and write everything again (in string) to record changes.

These aspects I mentioned are important. So for what you want to do it would look something like this:

// fazer o reset
["tbl_TABELA01", "tbl_TABELA02", "tbl_TABELA03"].forEach(function(str) {
    localStorage.setItem(str, '[]');
});


var nome = $("#lblNome").val();
var telefone = $("#lblTelefone").val();

var tbl_CELULAS = localStorage.getItem("tbl_CELULAS"); // buscar a string que está gravada
var dados = JSON.parse(tbl_CELULAS); // para ler e mudar como uma array
var novosDados = { // criar um novo objeto
    lbNome: nome,
    lbTelefone: telefone
};
dados.push(novosDados); // inserir na variável/array
var json = JSON.stringify(dados); // para voltar a ter tudo em string com os novos dados
localStorage.setItem("tbl_CELULAS", json); // escrever a string no localStorage

Note:

Do not use var JSON = JSON.stringify(tbl_CELULAS); ! you will be deleting the native method. As much as small print, to be another variable.

    
05.05.2016 / 20:09