0

Let's say I have the follow:

var test_data = {
'numGroup1': [[(1, 2, 3, 4, 5), (5, 6, 7, 8, 9)]],
'numGroup2': [[(10, 11, 12, 13, 14), (15, 16, 17, 18, 19)]],
};

How would I go about iterating through it using JavaScript?

5
  • for (var key in test_data) {} Commented May 24, 2013 at 4:36
  • 2
    You do know that the values become [[5, 9]] and [[14, 19]], right? Commented May 24, 2013 at 4:37
  • What is your expected output and purpose of iteration? Commented May 24, 2013 at 4:49
  • Ah, I didn't know that the parenthesis were causing that. I was just given data in that format. Commented May 24, 2013 at 5:01
  • 1
    @Josh Yeah, the comma operator causes the expression to evaluate to its last operand Commented May 24, 2013 at 5:04

2 Answers 2

1
var test_data = {
    'numGroup1': [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9]],
    'numGroup2': [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]],
};

for(var key in test_data){
    group = test_data[key];
    for(var num in group){
        console.log(group[num]);
    }    
}

@Ian is right... using () will not do anything but enter the last digit of each group. You should use a multidimensional array

        'numGroup1': [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9]],
        'numGroup2': [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]],
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks combining this with the lan's response solved the problem.
0

You can use underscorejs to iterate over it

var test_data = {
    'numGroup1': [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9]],
    'numGroup2': [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]],
};

_.chain(test_data).map(function(value, key) {
                   return value;
                }).flatten().each(alert);

1 Comment

Thanks, I like the underscore variation too, but I'll give it to Jay as it was the first one to solve my problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.