简体   繁体   English

如何使用JavaScript遍历此对象?

[英]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? 我将如何使用JavaScript进行迭代?

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. @Ian是正确的...使用()不会做任何事情,而是输入每个组的最后一位。 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]],

You can use underscorejs to iterate over it 您可以使用underscorejs对其进行迭代

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM