简体   繁体   English

如何使用jQuery或JavaScript遍历数组数组?

[英]How to loop through an array of arrays using jQuery or JavaScript?

I have a json array like this 我有一个像这样的json数组

var data = key: [
    {
        arr1: [
            {value: 1},
            {value: 2},
            {value: 3},
        ]
    },
    {
        arr2: [
            {value: 1},
            {value: 2},
            {value: 3},
        ]
    },
    {
        arr3: [
            {value: 1},
            {value: 2},
            {value: 3},
        ]
    }
]

I wanted to loop through key which is 3 times, and inside the key loop I wanted loop again through each of the elements inside key . 我想通过循环key是3倍的,关键循环中我通过各内部要素也想再要循环key

First I tried $.each which works fine for key array. 首先,我尝试了$.each ,它可以很好地用于键数组。 And inside the each function I tried to get the key values using Object.key(this) inside the loop which returned me the names 在每个函数中,我尝试在循环中使用Object.key(this)获取键值,该循环返回了名称

arr1, arr2, arr3

But I'm not able to loop through the names I got. 但是我无法遍历我得到的名字。

$.each(data, function(i, e) {
    Object.keys(this).each(function(index, element) {
        console.log(element);
    })    
})

I don't know if this is the right way of doing it, when I tried doing it this was. 我不知道这样做是否正确,当我尝试这样做时。 I got an error telling .each is not a function. 我在告诉.each不是函数时遇到错误。

Any help will be much appreciated. 任何帮助都感激不尽。

The simple way to do this would be to use three loops, but trying to make that work with this taking on a new meaning in each loop would be unnecessarily complicated. 最简单的方法来做到这一点是使用三个环路,而是试图使之与工作this在每个循环新的含义措施会被不必要地复杂化。

I recommend using .forEach and giving a clear name to each loop variable: 我建议使用.forEach并给每个循环变量一个明确的名称:

 var data = [ { arr1: [ {value: 1}, {value: 2}, {value: 3}, ] }, { arr2: [ {value: 1}, {value: 2}, {value: 3}, ] }, { arr3: [ {value: 1}, {value: 2}, {value: 3}, ] } ] data.forEach(function (outerObj) { Object.keys(outerObj).forEach(function (key) { outerObj[key].forEach(function (item) { console.log(item); }); }); }); 

Of course, there may be a better solution if you would actually tell us what you wanted to accomplish with these loops. 当然,如果您实际上告诉我们您想用这些循环完成什么,可能会有更好的解决方案。

If you want to keep with the jQuery pattern, then just keep using each for each nested level: 如果您想使用jQuery模式,那么只需为每个嵌套级别使用each模式即可:

 var data = [{arr1: [{value: 1}, {value: 2}, {value: 3}]}, {arr2: [{value: 1}, {value: 2}, {value: 3}]}, {arr3: [{value: 1}, {value: 2}, {value: 3}]}]; $.each(data, function(i, e) { $.each(e, function(key, arr) { console.log(key); $.each(arr, function(index, obj) { console.log("...", index, obj.value); }); }); }) 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

I believe the reason your attempt is not working is because you are trying to .each the individual values themselves, rather than the arrays within data . 我相信你尝试不工作的原因是因为你正在努力.each个体值本身,而不是在阵列中data

This should work: 这应该工作:

$.each(data, function(index, array) { // This each iterates over the arrays.
  $.each(array, function(subindex, value) { // This each iterates over the individual values.
    console.log(value); // Logs the individual values.
  });
});

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

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