admin管理员组文章数量:1413614
If I have a set of key/value pairs like this:
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
that I want to access as an array by the integer code, what is the best way to format the array? If I try this
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"}];
And I try to access the array to get, say "Haze" like so, I don't get what I want. I want to access the array by the key's integer value, not the position in the array
arrWXcodes["04"]
undefined
arrWXcodes[04]
Object { 21: "Precip detected in last hour" }
What would be the best data structure and access method to be able to use an integer key to access the array and get the expected value?
If I have a set of key/value pairs like this:
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
that I want to access as an array by the integer code, what is the best way to format the array? If I try this
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"}];
And I try to access the array to get, say "Haze" like so, I don't get what I want. I want to access the array by the key's integer value, not the position in the array
arrWXcodes["04"]
undefined
arrWXcodes[04]
Object { 21: "Precip detected in last hour" }
What would be the best data structure and access method to be able to use an integer key to access the array and get the expected value?
Share Improve this question asked May 12, 2015 at 14:43 Julian RaceJulian Race 711 silver badge7 bronze badges 1- JavaScript objects are just a collection of key/value pairs so James answer below seems to suit your needs. – Craicerjack Commented May 12, 2015 at 14:47
2 Answers
Reset to default 8Drop the array of objects and just have one main object:
var arrWXcodes = {
"00": "No significant weather",
"04": "Haze",
...
}
You can then access them properties by using arrWXcodes["00"]
, etc.
The reason you get a different result than expected in your own code when passing in an integer is because doing this references the index of the array and not the property name. 0
in the above example is "No significant weather"
, whereas "Haze"
is 1
and not 4
. Index 4
(the 5th item in the array) is your object with the value "Precip detected in last hour"
.
If you really want to be able to access the values with an integer you can convert the number to a string by using "" + 4
, however this will generate "4"
and not "04"
, so if your key names are in that structure you'd need to implement something like this: How can I pad a value with leading zeros?
Basically, you are defining an array of objects. And if you go by index 04
is the 5th element: {21:"Precip detected in last hour"}
. And the index "04"
is not defined in an array. An Array is like an object with integer key and its values: arrWXcodes={0:{00:"No significant weather"},1:{04:"Haze"}...}
Instead of using an array, you should use an object
arrWXcodes={
"00":"No significant weather",
....
};
本文标签: JavaScript access array elements by object valueStack Overflow
版权声明:本文标题:JavaScript access array elements by object value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744780471a2624678.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论