如果我有这样一组键/值对:的JavaScript访问数组的对象值的元素
WX Code WMO WX Code Description
------- -----------------------
00 No significant weather
04 Haze
10 Mist
20 Fog detected in last hour
21 Precip detected in last hour
22 Drizzle detected in last hour
23 Rain detected in last hour
24 Snow detected in last hour
,我想用整数代码访问作为数组,什么是格式的最好方式数组? 如果我试试这个
var arrWXcodes = [
{00:"No significant weather"},
{04:"Haze"},
{10:"Mist"},
{20:"Fog detected in last hour"},
{21:"Precip detected in last hour"},
{22:"Drizzle detected in last hour"},
{23:"Rain detected in last hour"},
{24:"Snow detected in last hour"}];
我尝试访问阵列来获得,说“雷雨”像这样,我没有得到我想要的东西。我想关键的整数值,而不是数组中的位置来访问阵列
arrWXcodes["04"]
undefined
arrWXcodes[04]
Object { 21: "Precip detected in last hour" }
什么是最佳的数据结构和访问方法,以便能够使用一个整数键进入阵列和获得期望值?
JavaScript对象仅仅是键/值对的集合似乎适合您的需求。 – Craicerjack