1

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?

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I cant call ["Large"] since i don't know the values at run-time
@adardesign: Ok. As a rule of thumb, use arrays when you are representing lists of data you want to process as a batch and objects when you want to lookup fast by a property. You can have arrays nested inside objects or vice versa based on the above rule.
2

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?

5 Comments

Can you please explain? how can i get the 3rd array?
in your answer i see the last example.. >>> arr[0][1]... Can you please add a 3rd nest example?
@adardesign: alright, added the 3rd nest. You can have a 4th one too, in my example, just by adding more indexing
Correct. My mistake.. May i ask you? is @Chetan correct that objects will make more sense?
@adardesign: it's hard to say without knowing your real application, but the way your data looks he may well be right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.