如果我有這樣一組鍵/值對:的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