如何循环访问另一个数组中每个数组的每个成员?

发布于 2024-11-25 04:40:15 字数 238 浏览 2 评论 0原文

假设有一个数组的数组。

n = [ a = ["foo","bar"],
      b = ["foo2","bar2"],
      c = ["foo3","bar3"],
      d = ["foo4","bar4"],
      e = ["foo5","bar5"] ]

循环所有 foobar 的最简单语法是什么?

Say there's an array of arrays.

n = [ a = ["foo","bar"],
      b = ["foo2","bar2"],
      c = ["foo3","bar3"],
      d = ["foo4","bar4"],
      e = ["foo5","bar5"] ]

What's the simplest syntax to loop through all the foos and bars?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏有森光若流苏 2024-12-02 04:40:15

如果按照您的示例,内部数组中总是有 2 个东西(小提琴 - http://jsfiddle.net/pvqtz/ ):

for (var index = 0; index < n.length; index++) {
    var foobar = n[index];
    var foo = foobar[0];
    var bar = foobar[1];
    alert(foo + bar);
}

对于任意数量的事物,使用嵌套循环(fiddle - http://jsfiddle.net/pvqtz/1/):

for (var index = 0; index < n.length; index++) {
    var innerArray = n[index];
    for (var innerIndex = 0; innerIndex < innerArray.length; innerIndex++) {
        var elem = innerArray[innerIndex];
        alert(elem);
    }
}

If you always have exactly 2 things in the inner arrays as per your example (fiddle - http://jsfiddle.net/pvqtz/):

for (var index = 0; index < n.length; index++) {
    var foobar = n[index];
    var foo = foobar[0];
    var bar = foobar[1];
    alert(foo + bar);
}

For an arbitrary number of things, use a nested loop (fiddle - http://jsfiddle.net/pvqtz/1/):

for (var index = 0; index < n.length; index++) {
    var innerArray = n[index];
    for (var innerIndex = 0; innerIndex < innerArray.length; innerIndex++) {
        var elem = innerArray[innerIndex];
        alert(elem);
    }
}
黯然#的苍凉 2024-12-02 04:40:15

如果你的环境支持它,我倾向于使用 forEach 来实现这一点:

n.forEach(function(n2) { 
    n2.forEach(function(val) {
        console.log(val) 
    })
});

而且,如果你正在处理任意深度的嵌套数组,你可以递归:

function visit(a, f) {
    if (Array.isArray(a)) {
        a.forEach(function(a2) { 
            visit(a2, f)
        })
    } else f(a);
}

n = ["foo",["foo1", "bar1"], ["foo2", ["foo3","bar3"]]];
visit(n, function(val) { console.log(val) });

If your environment supports it, I tend to like forEach for this:

n.forEach(function(n2) { 
    n2.forEach(function(val) {
        console.log(val) 
    })
});

and just for the heck of it, if you're dealing with nested arrays of arbitrary depth, you can recurse:

function visit(a, f) {
    if (Array.isArray(a)) {
        a.forEach(function(a2) { 
            visit(a2, f)
        })
    } else f(a);
}

n = ["foo",["foo1", "bar1"], ["foo2", ["foo3","bar3"]]];
visit(n, function(val) { console.log(val) });
锦欢 2024-12-02 04:40:15

要在一个循环中迭代 foos 和 Bars:

var i = -1;
while (i++ < n.length) 
    alert (n[i][0]+":"+n[i][1]);

for (var i = 0; i < n.length; i++) 
    alert (n[i][0]+":"+n[i][1]);

要迭代所有 foos,then 迭代所有 Bars:

var i = -1, j = -1;
while (i++ < 2) 
    while (j++ < n.length) 
        alert (n[j][i]);

for (var i = 0; i < 2; i++) 
    for (var j = 0; j < n.length; j++) 
        alert (n[j][i]);

To iterate over foos and bars in one loop:

var i = -1;
while (i++ < n.length) 
    alert (n[i][0]+":"+n[i][1]);

or

for (var i = 0; i < n.length; i++) 
    alert (n[i][0]+":"+n[i][1]);

To iterate over all foos, then over all bars:

var i = -1, j = -1;
while (i++ < 2) 
    while (j++ < n.length) 
        alert (n[j][i]);

or

for (var i = 0; i < 2; i++) 
    for (var j = 0; j < n.length; j++) 
        alert (n[j][i]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文