Print JSON.Parse

-1

Good morning,

I have a variable that stores data coming from a localStorage , which in turn contains the following data:

Array[3]
0:"{"Token":-6742.075757575755,"Solicitacao":"3359659","Justificativa":"jjjj"}"
1:"{"Token":-57645.84848484848,"Solicitacao":"10","Justificativa":""}"
2:"{"Token":-57645.84848484848,"Solicitacao":"10","Justificativa":"asdasd"}"
length:3

Code :

    var tbHistoricos = window.localStorage.getItem("tbHistoricos");
    tbHistoricos = JSON.parse(tbHistoricos);

What I want is to print the contents of each line by tag. For each line I print with:

tbHistoricos[numero_que_eu_quero]

But I do not want the whole line, I want every tag individual, but in the following way it does not work:

tbHistoricos[numero_que_eu_quero].Token

How can I do this?

    
asked by anonymous 05.01.2017 / 14:40

1 answer

2

Your problem is because the content of each position is a string and not an object, so you were not able to access it with .Token.

$ node
> var arr = [{"Token":-6742.075757575755,"Solicitacao":"3359659","Justificativa":"jjjj"},
{"Token":-57645.84848484848,"Solicitacao":"10","Justificativa":""},
{"Token":-57645.84848484848,"Solicitacao":"10","Justificativa":"asdasd"}]

> arr[0].Token
-6742.075757575755

Then in this case you need to parse every position

var arr = window.localStorage.getItem("tbHistoricos");
arr = JSON.parse(arr);
arr = arr.map(i => JSON.parse(i));
console.log(arr[0].Token);

Yes, it will work correctly.

    
05.01.2017 / 14:46