admin管理员组文章数量:1287776
I have 2 arrays that I want a Cartesian product of. As an example:
Customer Array:
[10,A]
[11,B]
Debtor Array:
[88,W]
[99,X]
I want to produce a new customerDebtor
array with:
[10,A,88,W]
[10,A,99,X]
[11,B,88,W]
[11,B,99,X]
I am attempting it with this code:
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
});
}
}
I have 2 arrays that I want a Cartesian product of. As an example:
Customer Array:
[10,A]
[11,B]
Debtor Array:
[88,W]
[99,X]
I want to produce a new customerDebtor
array with:
[10,A,88,W]
[10,A,99,X]
[11,B,88,W]
[11,B,99,X]
I am attempting it with this code:
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
});
}
}
Share
Improve this question
edited Sep 29, 2013 at 17:25
Ry-♦
225k56 gold badges492 silver badges498 bronze badges
asked Sep 29, 2013 at 17:15
GregGreg
2,6903 gold badges38 silver badges60 bronze badges
4 Answers
Reset to default 3You don't need jquery for this:
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
will be
[ [ 10, 'A', 88, 'W' ],
[ 10, 'A', 99, 'X' ],
[ 11, 'B', 88, 'W' ],
[ 11, 'B', 99, 'X' ] ]
concatMap works well here:
(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]);
})();
Returning:
[["alpha", 1], ["alpha", 2], ["alpha", 3], ["beta", 1], ["beta", 2], ["beta", 3], ["gamma", 1], ["gamma", 2], ["gamma", 3]]
2017 version
Using you data:
// Customer Array:
let c = [[10, 'A'], [11, 'B']];
// Debtor Array:
let d = [[88, 'W'], [99, 'X']];
all you need is one line:
let r = [].concat(...c.map(c => (d.map(d => c.concat(d)))));
or this for patibility with older browsers that don't support the spread syntax:
let r = [].concat.apply([], c.map(c => (d.map(d => c.concat(d)))));
You can see that console.log(r);
prints:
[ [ 10, 'A', 88, 'W' ],
[ 10, 'A', 99, 'X' ],
[ 11, 'B', 88, 'W' ],
[ 11, 'B', 99, 'X' ] ]
which is exactly what you wanted.
No loops, no pushes to arrays, no jQuery, just a few simple function calls.
For more than two arrays, see this answer:
- Cartesian product of multiple arrays in JavaScript
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 ponents (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"]]
本文标签: javascriptCartesian product of 2 arraysStack Overflow
版权声明:本文标题:javascript - Cartesian product of 2 arrays - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741326469a2372511.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论