美文网首页
JS 笛卡尔积 算法

JS 笛卡尔积 算法

作者: TouchMe丶 | 来源:发表于2018-09-15 15:06 被阅读22次
    function calcDescartes (array) {
        if (array.length < 2) return array[0] || [];
        return [].reduce.call(array, function (col, set) {
            var res = [];
            col.forEach(function (c) {
                set.forEach(function (s) {
                    var t = [].concat(Array.isArray(c) ? c : [c]);
                    t.push(s);
                    res.push(t);
                })
            });
            return res;
        });
    }
    
    console.log(calcDescartes([[1,2,3],['a','b','c']]));
    

    来自 http://www.while0.com/222.html

    相关文章

      网友评论

          本文标题:JS 笛卡尔积 算法

          本文链接:https://www.haomeiwen.com/subject/gbttnftx.html