Why do associative arrays don't work in localStorage[""]? Why do associative arrays don't work in localStorage[""]? google-chrome google-chrome

Why do associative arrays don't work in localStorage[""]?


localStorage stores values as strings, so you need to JSON serialize your objects on the way in and deserialize them on the way out. For example:

var data = {'A': 9};localStorage['screenshots'] = JSON.stringify(data);// Later/elsewhere:var data = JSON.parse(localStorage['screenshots']);// 9console.log(data.A);


The localStorage object can only store strings. To store other types of data, use must convert them to strings, and convert them back on retrieval. In most cases you would want to use JSON to do this.


Local storage only stores string keys and string values.

The DOM Storage mechanism is a means through which string key/value pairs can be securely stored and later retrieved for use.

Source: MDC.