电商SKU全排列算法

let names = ["iphone X", "iphone XS"];
let colors = ["黑色", "白色"];
let storages = ["64g", "256g"];
let combine = function (...chunks) {
    let res = []
    let helper = function (chunkIndex, prev) {
        let chunk = chunks[chunkIndex];
        let isLast = chunkIndex === chunks.length - 1
        for (let val of chunk) {
            let cur = prev.concat(val)
            if (isLast) {
                res.push(cur)
            } else {
                helper(chunkIndex + 1, cur)
            }
        }
    }
    helper(0, [])
    return res;
}
console.log(combine(names, colors, storages));

 

posted @ 2020-06-17 14:15  问问大将军  阅读(556)  评论(0编辑  收藏  举报