Skip to content
Fix Code Error

Storing Objects in HTML5 localStorage

March 13, 2021 by Code Error
Posted By: Anonymous

I’d like to store a JavaScript object in HTML5 localStorage, but my object is apparently being converted to a string.

I can store and retrieve primitive JavaScript types and arrays using localStorage, but objects don’t seem to work. Should they?

Here’s my code:

var testObject = { 'one': 1, 'two': 2, 'three': 3 };
console.log('typeof testObject: ' + typeof testObject);
console.log('testObject properties:');
for (var prop in testObject) {
    console.log('  ' + prop + ': ' + testObject[prop]);
}

// Put the object into storage
localStorage.setItem('testObject', testObject);

// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');

console.log('typeof retrievedObject: ' + typeof retrievedObject);
console.log('Value of retrievedObject: ' + retrievedObject);

The console output is

typeof testObject: object
testObject properties:
  one: 1
  two: 2
  three: 3
typeof retrievedObject: string
Value of retrievedObject: [object Object]

It looks to me like the setItem method is converting the input to a string before storing it.

I see this behavior in Safari, Chrome, and Firefox, so I assume it’s my misunderstanding of the HTML5 Web Storage spec, not a browser-specific bug or limitation.

I’ve tried to make sense of the structured clone algorithm described in http://www.w3.org/TR/html5/infrastructure.html. I don’t fully understand what it’s saying, but maybe my problem has to do with my object’s properties not being enumerable (???)

Is there an easy workaround?


Update: The W3C eventually changed their minds about the structured-clone specification, and decided to change the spec to match the implementations. See https://www.w3.org/Bugs/Public/show_bug.cgi?id=12111. So this question is no longer 100% valid, but the answers still may be of interest.

Solution

Looking at the Apple, Mozilla and Mozilla again documentation, the functionality seems to be limited to handle only string key/value pairs.

A workaround can be to stringify your object before storing it, and later parse it when you retrieve it:

var testObject = { 'one': 1, 'two': 2, 'three': 3 };

// Put the object into storage
localStorage.setItem('testObject', JSON.stringify(testObject));

// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');

console.log('retrievedObject: ', JSON.parse(retrievedObject));
Answered By: Anonymous

Related Articles

  • How to prevent scrolling the whole page?
  • How to use QThread() within class of QWidget function?
  • After the login click i am not able to render the data that…
  • Export more than one variable in ES6?
  • setTimeout function not working : javascript
  • Use data from session storage web in Polymer
  • How can I access and process nested objects, arrays or JSON?
  • How to get JS variable to retain value after page refresh?
  • What are the nuances of scope prototypal / prototypical…
  • Three.js: Cannot display mesh created with texture array

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to write a:hover in inline CSS?

Next Post:

How to query MongoDB with “like”?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error