JavaScript 笛卡尔积算法,可用于商品 SKU 计算

MaxSky 等级 291 1 0

代码示例:https://www.helloworld.net/redirect?target=https://github.com/maxsky/imitate-tb-combo-select

Demo 演示:https://www.helloworld.net/redirect?target=https://codepen.io/maxsky/pen/oNLWgNp

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']]));

在 Chrome 运行一下:

JavaScript 笛卡尔积算法,可用于商品 SKU 计算

收藏
评论区