0

What i get in localStorage is [object Object] but for other variables in javascript it does work.

Sample Code:

var some_hash = {
"key1":"value1",
"key2": "value2",
"key3" : [{
"key3a": "value3a"}]


}

var deep_copy = $.extend(true, {}, some_hash)
console.log(deep_copy)
Object {key1: "value1", key2: "value2", key3: Array[1]}

localStorage["help"] = $.extend(true, {}, some_hash)
console.log(localStorage["help"])
[object Object]

Why is that ? Any workaround ?

igauravsehrawat
  • 3,696
  • 3
  • 33
  • 46

1 Answers1

3

localStorage only stores values as strings, not objects.

Try using JSON.stringify and JSON.parse:

 var deep_copy = JSON.stringify($.extend(true, {}, some_hash));
 console.log(JSON.parse(deep_copy));

See here, too: Storing Objects in HTML5 localStorage

Community
  • 1
  • 1
Steve Hynding
  • 1,799
  • 1
  • 12
  • 22