- Written by Chad Campbell on Feb 15th 2012
With the HTML Local Storage feature, you have a more powerful approach to storing information over the traditional cookie. At first glance, this feature appears to only work with strings. But, you may need to store something more complex. Like a collection, or an array of JSON objects. That was the situation I ran into when I got an education on this feature.
Storing an Array in Local Storage
To store a collection or array in local storage, you must first convert it to a string. To do that, you can use something like the following:
In the previous snippet, we took a JSON array and we made it into a string. This process of converting it to a string was handled by the ever-popular, ever-useful JSON.stringify function. This native function in modern browsers handles the process of serializing the data. To get an array from local storage and convert it back to JSON is covered here.