In general, the problem is: I get an array from localStorage , output it, and double quotes in which the array elements are framed are automatically escaped, how to remove it?

localStorage.setItem('idsGTag', JSON.parse(idsGTag)); localStorage.setItem('totalGTag', JSON.stringify(totalGTag)); var idsGTag = localStorage.getItem('idsGTag'); var totalGTag = localStorage.getItem('totalGTag'); 

I have an Uncaught SyntaxError error: Unexpected token u in JSON at position 0. But if I just get the variables from the repository and output to the console, then everything is ok. I intentionally use parse and stringify to see the result of both

  • one
    I suspect when getting you need to use JSON.parse - Grundy
  • I guess I'm doing something wrong. localStorage.setItem ('idsGTag', JSON.parse (idsGTag)); localStorage.setItem ('totalGTag', JSON.stringify (totalGTag)); var idsGTag = localStorage.getItem ('idsGTag'); var totalGTag = localStorage.getItem ('totalGTag'); I have an Uncaught SyntaxError error: Unexpected token u in JSON at position 0. But if I just get the variables from the chronishing and output it in the console, then everything is ok. I intentionally use parse and stringify to see the result of both - mydls1

1 answer 1

The correct solution has this code.
I did it

  1. get variable from local storage

     var P1= localStorage.getItem('perem1'); var P2= localStorage.getItem('perem2'); 
  2. do json parse

     var gid = JSON.parse(P1); var gpr = JSON.parse(P2); 
  3. work with gid and gpr variables