Joshua
Joshua

Reputation: 1270

How do I iterate through this object using JavaScript?

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?

Upvotes: 0

Views: 68

Answers (2)

Fizer Khan
Fizer Khan

Reputation: 92905

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);

Upvotes: 0

JayMoretti
JayMoretti

Reputation: 265

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]],

Upvotes: 1

Related Questions