js實現超簡單sku組合算法(不同屬性的排列組合)


 

let arr = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12],
];
 
function cartesianProductOf() {
    return Array.prototype.reduce.call(arguments,       function(a, b) {
        var ret = [];
        a.forEach(function(a) {
            b.forEach(function(b) {
                ret.push(a.concat([b]));
            });
        });
        return ret;
    }, [[]]);
}
 
let allArr =cartesianProductOf(...arr )
console.log(allArr)

 

 

此算法類似笛卡爾積

 

 

 

轉: https://www.cnblogs.com/hpx2020/p/10723192.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM