Array cartesian Product - Node.js Array

Node.js examples for Array:Set Operation

Description

Array cartesian Product

Demo Code

// http://gotochriswest.com/blog/2011/05/02/cartesian-product-of-multiple-arrays/
function cartesianProduct(arr) {
    return Array.prototype.reduce.call(arr, function(a, b) {
        var ret = [];
        a.forEach(function(a) {
            b.forEach(function(b) {
                ret.push(a.concat([b]));
            });/*  w  w w.  j  a  v  a  2 s.com*/
        });
        return ret;
    }, [[]]);
};

function zeros(n) {
    var a = [];
    while (--n >= 0) {
        a[n] = 0;
    }
    return a;
};

Related Tutorials