0

I have several large multi-dimensional arrays that I'm saving to local storage. They look like this:

[[[-150],[0],[-650],0],[[-100],[0],[-650],0],[[-50],[0],[-650],0] ... ]

The data is '4D' data. When I try loading this 'string' into an array in another JS (in separate html), it doesn't behave like an array- it's only a string.

Here's how I load the data back into the second JS (not sure why the loop didn't work either):

var lay= new Array();

//for(var g=0;g>=9;g++)
//{   lay[g] = localStorage.getItem("key" + g);
//  console.log(lay[g]);
//} 
lay[0] = localStorage.getItem("key0"); 
lay[1] = localStorage.getItem("key1");
lay[2] = localStorage.getItem("key2");
//... more here 
lay[9] = localStorage.getItem("key3");

After I load this "4D" info into the array, I pull the info out to use it:

var count=0;
var len=0;

for (y=0;y<=1;y++)
{   console.log(lay[y]);
count=1;
len = lay[y].length;

for (x=1;x<=len-1;x++)
{
        Rx =     lay[y][count][0];
        Ry =     lay[y][count][1];
        Rz =     lay[y][count][2];
        Rcolor = lay[y][count][3];

When I add this to the code console.log(len); I get the length of characters in the array, not the number of elements. How can I get the data from local storage to come in and behave like array? I thought that the formatting alone would get it behave like an array.

Do I need to parse it back into an array again? If so, I'm guessing I should just output the data in a simpler format to parse it again...

Thanks for the help!

Edit

Here's how I made the local storage:

for (var a=0;a<=14;a++)
{   updateTemp(tStep + a);

$("#temp tbody tr").each(function(i, v){
    data[i] = Array();
    $(this).children('td').each(function(ii, vv){
        data[i][ii] = $(this).text();       
        rows=ii;
        cols=i;
    }); 

});
   retval="";
for (var q=0;q<=cols;q++)
{
    for (var w=0;w<=rows;w++)
    {
        var tempv = data[q][w];
        var tX = w*50 - 1000;
        var tY = 1*50 - 50;
        var tZ = q*50 - 1000;
        if (tempv==-9){
            (dummy=q*w);
        } 
        else {retval +=  tX +',' + tY + ',' + tZ + ',' + tempv + ',';}      
    }
} 
    var kee = "key" + a;
retval = retval.substring(0, retval.length-1); //this is to get rid of the last character which is an extra ,
window.localStorage.setItem(kee, retval);}
Community
  • 1
  • 1
Crimpy
  • 195
  • 20
  • 2
    Wouldn't it be easier to serialize your array into string, save the string and then deserialzie it upon retreival? – Yuriy Galanter May 12 '14 at 19:33
  • 1
    You can't store arrays in localStorage, only strings, so when you pass in an array .toString() is applied, and when you get it back it's a string. To stringify the array use JSON.stringify, and JSON.parse to get it back – adeneo May 12 '14 at 19:34
  • *"I get the length of characters in the array, not the number of elements."* that's because what you refer to as "the array" is actually a string representation of the array. we need to see how you are adding these values to localstorage. – Kevin B May 12 '14 at 19:36

2 Answers2

3

JSON encode the array before storing, parse after retrieving.

localStorage.test = JSON.stringify([1,2,3]);
console.log(JSON.parse(localStorage.test));
Syntax
  • 2,073
  • 15
  • 15
  • so in my output string to local storage, can I just make it a string of CSV's? – Crimpy May 12 '14 at 19:40
  • @Crimpy, you could use `Array.join` and `String.split` instead of `JSON`, yes. When dealing with multi-dimensional arrays, I suggest JSON though. – Syntax May 12 '14 at 19:42
  • I read an html table to get the data in to the array. I'm not sure how I use the JSON.strigify in this context. See my edits above. – Crimpy May 12 '14 at 19:52
-2

This is a duplicate of “Storing Objects in HTML5 localStorage”. localstorage only handles strings. As suggested there, serialise your array to a JSON string before storing.

Community
  • 1
  • 1
Buck Doyle
  • 6,333
  • 1
  • 22
  • 35