javascript Array 索引 僅能用數值型態,物件的索引才能用字串型態的。


Javascript arrays cannot have “string indexes". A Javascript Array is exclusively numerically indexed. When you set a “string index", you’re setting a property of the object. These are equivalent:

array.a = ‘foo’;
array[‘a’] = ‘foo’;

Those properties are not part of the “data storage" of the array.

If you want “associative arrays", you need to use an object:

var obj = {};
obj[‘a’] = ‘foo’;

Maybe the simplest visualization is using the literal notation instead of new Array:

// numerically indexed Array
var array = [‘foo’, ‘bar’, ‘baz’];

// associative Object
var dict = { foo : 42, bar : ‘baz’ };



在下方填入你的資料或按右方圖示以社群網站登入: Logo

您的留言將使用 帳號。 登出 /  變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )


您的留言將使用 Facebook 帳號。 登出 /  變更 )


連結到 %s