Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i store array of array in localstorage which are different in size?
Posted
Comments
Sergey Alexandrovich Kryukov 1-May-14 13:30pm    
What's wrong with just reading any HTML5 manual? This matter should be apparent after reading about local storage.
—SA

1 solution

The local storage is based on name/value pairs, like all associative containers in Javascript (essentially all Javascript object, including arrays, are such associative arrays):
http://en.wikipedia.org/wiki/JavaScript#Dynamic[^],
http://en.wikipedia.org/wiki/Associative_arrays[^].

Please see how it works: http://diveintohtml5.info/storage.html[^].

The associated container does not have certain size: the size grows as you add data. Let's see how it works with a single array indexed by integers:
JavaScript
localStorage[10] = "the value @10";
// if your local storage was empty before, you have only one element (not 11 or 10 of them)

localStorage[111] = "the value @111";
// only two elements for now

alert(localStorage[10]); // will display "the value @10"


Likewise, you can have array of arrays, which you can simply interpret as the array indexed with compound index, which can be, in this case, just a pair of indices:
JavaScript
localStorage[1,13] = "the value @[1,13]"; 
localStorage[111] = "the value @112"; // you can even combine 1D and 2D arrays
alert(localStorage[1,13]); // will display "the value @[1,13]"

By definition, you will have more flexibility than you expected: you will get jagged arrays: http://en.wikipedia.org/wiki/Jagged_array[^].

That's all.

—SA
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900