您可以使用数组方法,例如concat
达到同样的结果。
代码片段:
function sqlCrossJoin(arr1, arr2) {
arr1 = arr1 || [['jan', 1], ['feb', 2]];
arr2 = arr2 || [
['Jane Doe', 1983, 'PortLand', 'Oregon'],
['John Smith', 1984, 'San Francisco', 'California'],
];
var output = [];
arr1.forEach(function(month) {
arr2.forEach(function(customer) {
output.push(month.concat(customer));
});
});
return output;
/* Expected Output:
[ [ 'jan', 1, 'Jane Doe', 1983, 'PortLand', 'Oregon' ],
[ 'jan', 1, 'John Smith', 1984, 'San Francisco', 'California' ],
[ 'feb', 2, 'Jane Doe', 1983, 'PortLand', 'Oregon' ],
[ 'feb', 2, 'John Smith', 1984, 'San Francisco', 'California' ] ]*/
}
参考: