0

In my code I am doing a lot of the following:

var store = window.localStorage; var accountID = store.getItem('AccountID'), pageID = store.getItem('PageID'), referenceID = store.getItem('ReferenceID'), topicID = store.getItem('TopicID'); 

Is there a way I could do this more efficiently. For example could I create an object in Javascript and store / get the whole object at once?

1 Answer 1

3

Well, if you try it, you'll find that you can't. Whatever you store will be set as its toString value.

One possibility depending on what data you're storing would be to stringify it as JSON data, then parse it when needed.

var data = {foo:"bar"}; window.localStorage.setItem("test", JSON.stringify(data)); var fetched = window.localStorage.getItem("test"); var parsed = JSON.parse(fetched); alert(parsed.foo); // "bar" 

Of course this will limit you to the types of data that are JSON compatible.

Sign up to request clarification or add additional context in comments.

Comments