Jump to content

Use localStorage as a multi-dimensional array


mjsulliv

Recommended Posts

Hello All. Can I use localStorage as a multi-dimensional array?eg:

 var menu = "selectMenu"; localStorage[menu][checkBoxName]="CHECKED";

When I do this I get a browser error: "localStorage[menu] is null"Thanks --- Mike

Link to comment
Share on other sites

you might just want to make your menu state an object and then just assign that to a property in localStorage. You can't make an array out of a string, since menu is string and your trying to access a member in it [menu][checkBoxName]

Link to comment
Share on other sites

1. So far as I know, you get to use the storage object's setItem and getItem methods2. Also so far as I know, all data gets stringified. So if I did this to set some data:localStorage.setItem("trial",[4,5,6]);and I did this when the page reloadsvar s = localStorage.getItem("trial");alert(s); // "4,5,6"alert(typeof s) // "string"It's recommended to store complex data in JSON format. See https://developer.mozilla.org/en/DOM/Storage

Link to comment
Share on other sites

1. So far as I know, you get to use the storage object's setItem and getItem methods2. Also so far as I know, all data gets stringified. So if I did this to set some data:localStorage.setItem("trial",[4,5,6]);and I did this when the page reloadsvar s = localStorage.getItem("trial");alert(s); // "4,5,6"alert(typeof s) // "string"It's recommended to store complex data in JSON format. See https://developer.mozilla.org/en/DOM/Storage
interesting, setters and getters are mentioned here, although that doesn't necessarily mean anything :)http://www.w3schools.com/html5/html5_webstorage.asp
Link to comment
Share on other sites

you might just want to make your menu state an object and then just assign that to a property in localStorage. You can't make an array out of a string, since menu is string and your trying to access a member in it [menu][checkBoxName]
Could you give a syntax example?
Link to comment
Share on other sites

1. So far as I know, you get to use the storage object's setItem and getItem methods2. Also so far as I know, all data gets stringified. So if I did this to set some data:localStorage.setItem("trial",[4,5,6]);and I did this when the page reloadsvar s = localStorage.getItem("trial");alert(s); // "4,5,6"alert(typeof s) // "string"It's recommended to store complex data in JSON format. See https://developer.mozilla.org/en/DOM/Storage
But isn't this still a one dimensional array? What I'd like is:localStorage[trial][lowNumbers] = "4,5,6";localStorage[trial][highNumbers] = "7, 8, 9";
Link to comment
Share on other sites

have you looked up JSON? It's very easy to learn. You can even make object literals.

localStorage.data = {trial : {lowNumbers : [4,5,6], highNumbers : [7,8,9]}};

it may or may not need to be stringified (for local storage use), but that is basic JSON syntax. in this an example, you have an object literal that has one property, trial, which has two properties of its own; lowNumbers and highNumbers, which are each arrays. so to get the number 5 and 9, you would write this:

var trial = {lowNumbers : [4,5,6], highNumbers : [7,8,9]};console.log(trial.lowNumbers[1]);  //logs 5console.log(trial.highNumbers[2]); //logs 9

Link to comment
Share on other sites

FWIW, these browsers have native JSON support, well documented at dev.mozilla: IE8+, Firefox 3.5+, Safari 5+, Chrome, and Opera 10+. These are not quite the same browsers that support localStorage.For older browsers, you can hardcode JSON strings and parse them using eval(), or add this library object that was the actual inspiration for the native JS object.

Link to comment
Share on other sites

have you looked up JSON? It's very easy to learn. You can even make object literals.
localStorage.data = {trial : {lowNumbers : [4,5,6], highNumbers : [7,8,9]}};

it may or may not need to be stringified (for local storage use), but that is basic JSON syntax. in this an example, you have an object literal that has one property, trial, which has two properties of its own; lowNumbers and highNumbers, which are each arrays. so to get the number 5 and 9, you would write this:

var trial = {lowNumbers : [4,5,6], highNumbers : [7,8,9]};console.log(trial.lowNumbers[1]);  //logs 5console.log(trial.highNumbers[2]); //logs 9

I've read briefly on JSON but have used it or looked at it in a while. I'll take a look.Thanks --- Mike
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...