Javascript 循环,该循环遍历每个数组的每个项,但仅针对第一个数组';s长度

Javascript 循环,该循环遍历每个数组的每个项,但仅针对第一个数组';s长度,javascript,arrays,loops,Javascript,Arrays,Loops,我有一个元素数组,如下所示: var result = [ // outer array [ // inner first array {name: 'A', value: 111}, {name: 'B', value: 222}, {name: 'C', value: 333}, ... ], [ // inner subsequent arrays #2 {name: '

我有一个元素数组,如下所示:

var result = [
// outer array
    [
    // inner first array
        {name: 'A', value: 111},
        {name: 'B', value: 222},
        {name: 'C', value: 333},
        ...
    ],
    [
    // inner subsequent arrays #2
        {name: 'D', value: 55},
        {name: 'E', value: 99},
        ...
    ],
    // inner subsequent arrays #3
        {name: 'F', value: 1000},
        ...
    ],
    ...
    ...
]
我想检查每个元素(
A-F
),但只检查第一个数组(
A-C
)的每个元素

像这样:

AA、AB、AC、AD、AE、AF

BB、BC、BD、BE、BF

CC、CD、CE、CF

编辑:我不知道任何数组的长度,因此无法使用任何常量


此外,它不仅仅是2个数组(上面更新的示例)。

@Fourtheye的解决方案非常聪明,但我认为它缺少了您问题的一个主要要求。以下是您可以修复它的方法:

// Reduce all the arrays into a single array
var firstArray = result[0],
    allItems = result.reduce(function(result, current) {
        return result.concat(current);
    }, []), i, j;

// Iterate till the end of the array
for (i = 0; i < firstArray.length; i += 1) {
    // Start from the current i and iterate till the end
    for(j = i; j < allItems.length; j += 1) {
        console.log(firstArray[i].name + allItems[j].name);
    }
}
//将所有数组缩减为单个数组
var firstArray=结果[0],
allItems=结果.reduce(函数(结果,当前){
返回结果concat(当前);
},[]),i,j;
//迭代到数组的末尾
对于(i=0;i
它不是有效的JavaScript对象。逻辑提示:在内部使用foreachforeach@thefourtheye不是一个对象,但它是一个有效的javascript数组。数组也是一个对象,所以可以说它是一个有效的JSobject@thefourtheye固定的。它是now@laggingreflex我更新了答案,请查收。您想要所有的组合吗?如果我弄错了,请原谅,但是只有当第一个数组包含正好一半的数组元素时,这不是才有效吗?我看不出问题中有任何保证条件为真的规定。@jlRise你可能是对的,我只包括了一般版本。我认为你的新答案也不正确。您的答案似乎只是基于提供的示例,而不是基于对问题的描述:“我希望遍历每个元素(A-F),但只针对第一个数组(A-C)的每个元素。”我将在第二次迭代中使用
I
,而不是在前一次迭代的基础上保存新数组
AA
AB
AC
AD
AE
AF
BB
BC
BD
BE
BF
CC
CD
CE
CF
...
...
// Reduce all the arrays into a single array
var firstArray = result[0],
    allItems = result.reduce(function(result, current) {
        return result.concat(current);
    }, []), i, j;

// Iterate till the end of the array
for (i = 0; i < firstArray.length; i += 1) {
    // Start from the current i and iterate till the end
    for(j = i; j < allItems.length; j += 1) {
        console.log(firstArray[i].name + allItems[j].name);
    }
}