javascript trying to get 3rd nested array.length and value
I have a generated nested Array which I store some data.
How can i get the 3rd nested array (in this case the array starting with "yellow")
the array looks like this (this is a dynamically generated array):
[
["Large",
["yellow", "green", "Blue"],
["$55.00", "$55.00", "$55.00"]
]
["Medium",
["yellow", "green", "Blue", "Red"],
["$55.00", "$55.00", "$55.00", "$55.00"]
]
["small",
["yellow", "green", "Blue", "Red"],
["$55.00", "$55.00", "$55.00", "$55.00"]
]
]
I am trying to get to the ["yellow", "green", "Blue"] array's length and loop to get the values
for(i=0; colorNSize.dataArray[0][0][1].length<i; i++){
alert(colorNSize.dataArray[colorNSize.Sizeindex][0][0][i])// alert's A (which is the char[1] of the second array.
}
alert(colorNSize.dataArray[0][0][1].length)
actually alerts the length of "Large" which is "5"
is there a limit for nested arrays?
开发者_开发技巧Can i still get the real 3rd array nested here?
Why does everything have to be an array? This structure makes much more sense to me.
obj = {
"Large": {
"yellow": "$55.00",
"green": "$55.00",
"Blue": "$55.00"
},
"Medium": {
...
...
]
To get the Large, yellow price, all I have to do is
obj["Large"]["yellow"] //or obj.Large.yellow
Your formatting is probably wrong - does "Large" reside in the same array as the array of colors and then prices? Indentation tells it is, brackets say it's all nested. Have you forgotten a bracket after the array of prices?
Anyway, [0][0][1]
already refers to chars of "Large". [0][0]
is "Large", [0][1]
is the other array.
Consider this simplified example:
>>> arr = [['large', [[1, 2], [3, 4]]]]
[["large", [[1, 2], [3, 4]]]]
>>> arr[0]
["large", [[1, 2], [3, 4]]]
>>> arr[0][0]
"large"
>>> arr[0][1]
[[1, 2], [3, 4]]
>>> arr[0][1][0]
[1, 2]
Can you see what's going on?
精彩评论