7

我有 2 个数组,我想要一个笛卡尔积。举个例子:

客户阵列:

[10,A]
[11,B]

债务人数组:

[88,W]
[99,X]

我想生成一个新customerDebtor数组:

[10,A,88,W]
[10,A,99,X]
[11,B,88,W]
[11,B,99,X]

我正在尝试使用以下代码:

for (var i = 0; i < customerArray.length; i++) {
    for (var l = 0; l < debtorArray.length; l++) {
        $.each(customerArray[i], function (ndx, val) {
            //???? push values into customerDebtorMatrix array
        });
    }
}
4

4 回答 4

3

为此,您不需要 jquery:

var customerArray = [[10,'A'],[11,'B']];
var debtorArray = [[88,'W'],[99,'X']];

var customerDebtorMatrix = [];
for (var i = 0; i < customerArray.length; i++) {
    for (var l = 0; l < debtorArray.length; l++) {
        customerDebtorMatrix.push(customerArray[i].concat(debtorArray[l]));
    }
}

customerDebtorMatrix将会

[ [ 10, 'A', 88, 'W' ],
  [ 10, 'A', 99, 'X' ],
  [ 11, 'B', 88, 'W' ],
  [ 11, 'B', 99, 'X' ] ]
于 2013-09-29T17:21:21.157 回答
3

concatMap 在这里运行良好:

(function() {
    'use strict';

    // cartProd :: [a] -> [b] -> [[a, b]]
    function cartProd(xs, ys) {
        return [].concat.apply([], xs.map(function (x) {
            return [].concat.apply([], ys.map(function (y) {
                return [[x, y]];
            }));
        }));
    }

    return cartProd(["alpha", "beta", "gamma"], [1, 2, 3]);

})(); 

返回:

[["alpha", 1], ["alpha", 2], ["alpha", 3], ["beta", 1], ["beta", 2], ["beta", 3], ["gamma", 1], ["gamma", 2], ["gamma", 3]]
于 2016-03-06T19:58:43.140 回答
1

2017版

使用您的数据:

// Customer Array:
let c = [[10, 'A'], [11, 'B']];
// Debtor Array:
let d = [[88, 'W'], [99, 'X']];

您只需要一行:

let r = [].concat(...c.map(c => (d.map(d => c.concat(d)))));

或者为了与不支持扩展语法的旧浏览器兼容:

let r = [].concat.apply([], c.map(c => (d.map(d => c.concat(d)))));

你可以看到console.log(r);打印:

[ [ 10, 'A', 88, 'W' ],
  [ 10, 'A', 99, 'X' ],
  [ 11, 'B', 88, 'W' ],
  [ 11, 'B', 99, 'X' ] ]

这正是你想要的。

没有循环,没有推送到数组,没有 jQuery,只有几个简单的函数调用。

对于两个以上的数组,请参阅此答案:

于 2017-03-27T17:09:22.850 回答
0

Actually this would be the Kronecker (or Tensor) Product

Javascript code for the kronecker product of 2 arrays

function kronecker_product(a, b) 
{
    var i, j, k, al = a.length, bl = b.length, abl = al*bl, ab = new Array(abl);
    i = 0; j = 0;
    for (k=0; k<abl; k++)
    {
        if ( j>=bl) {j=0; i++;}
        ab[k] = [].concat(a[i],b[j]);
        j++;
    }
    return ab;
}

For disambiguation let me give the code for the cartesian product as well:

function cartesian_product(a, b) 
{
    var al = a.length, bl = b.length;
    return [a.concat(bl>al ? new Array(bl-al) : []), b.concat(al>bl ? new Array(al-bl) : [])];
}

The main difference is the number of components (length) of the final result, in kronecker the length should be the product of the lengths while in cartesian it should be the sum.

Example Kronecker

var a = ["a","b"], b = ["c","d"];
console.log(kronecker_product(a,b));

Output

[["a", "c"], ["a", "d"], ["b", "c"], ["b", "d"]]

Example Cartesian

var a = ["a","b"], b = ["c","d"];
console.log(cartesian_product(a,b));

Output

 [["a", "b"], ["c", "d"]]
于 2015-02-25T17:23:36.650 回答