最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why do associative arrays don't work in localStorage[""]? - Stack Overflow

programmeradmin2浏览0评论

For example I have the following code:

  localStorage["screenshots"] = new Array();
  localStorage["screenshots"]["a"] = 9;
  alert(localStorage["screenshots"]["a"]);

  Arr = new Array();
  Arr["screenshots"] = new Array();
  Arr["screenshots"]["a"] = 9;
  alert(Arr["screenshots"]["a"]);

(I use Google Chrome v9.0.597.107 on Windows Vista 32-bit)

But only the second part works (output of alert() is "a")! The first alert outputs in contrast "undefined"!

What is the problem?

Thanks.

For example I have the following code:

  localStorage["screenshots"] = new Array();
  localStorage["screenshots"]["a"] = 9;
  alert(localStorage["screenshots"]["a"]);

  Arr = new Array();
  Arr["screenshots"] = new Array();
  Arr["screenshots"]["a"] = 9;
  alert(Arr["screenshots"]["a"]);

(I use Google Chrome v9.0.597.107 on Windows Vista 32-bit)

But only the second part works (output of alert() is "a")! The first alert outputs in contrast "undefined"!

What is the problem?

Thanks.

Share Improve this question asked Mar 7, 2011 at 15:21 ComFreekComFreek 29.4k18 gold badges107 silver badges157 bronze badges 6
  • 1 Unrelated to local storage issues, but JS has no such concept of associative arrays. When you make an array, and the add items via ["name"] syntax, you are really adding a property to that Array instance object. The array's length property does not change, nor is the array considered to have any items. What you ought to be doing is making an object if you aren't after an indexed array (the only sort of Array JS knows anything about). – JAAulde Commented Mar 7, 2011 at 15:32
  • 1 @JAAulde: that is an extremely misleading statement. JavaScript has objects, created with new Object() or object literal syntax {}, which are associative arrays. – Matt Ball Commented Mar 7, 2011 at 16:10
  • 1 @Matt No, they're objects. The site you link is mixing terms and is where the misleading takes place. You can treat them in the manner in which other languages treat associative arrays (string-named property accessing in square brackets) but they are NOT arrays. They do not have length properties which are maintained through manipulation, nor do they have methods associated with arrays such as push, shift, etc. JS only has one object which falls into the category of array, and that is the one constructed with Array(). It is an indexed array, not associative. – JAAulde Commented Mar 7, 2011 at 16:32
  • @JAAulde: what definition of associative array are you using? I don't see anything on, say Wikipedia, that says an associative array must have a length property or (integer-indexed) array methods. The basic methods are, as I understand it (and Wikipedia seems to agree with me): add, reassign, remove, and lookup. See also this article. – Matt Ball Commented Mar 7, 2011 at 16:39
  • @Matt, There are, admittedly, some semantics issues here. In computer science terms you're right that JS implements assoc arrays in the hash table mechanism used for its objects. I am talking JS specifics, though. Despite the fact that under the hood there are assoc arrays in use, JS doesn't make such a construct available to the developer by such a name. JS provides Objects, a subclass of which is an array ( Array() ). In JS when you use an Array it is because you need behavior such as I mentioned earlier. My first comment stemmed from what I believe is a misuse of that construct – JAAulde Commented Mar 7, 2011 at 16:55
 |  Show 1 more comment

3 Answers 3

Reset to default 17

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']);

// 9
console.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.

发布评论

评论列表(0)

  1. 暂无评论