首页 > 其他 > 详细

商品sku的排列组合

时间:2020-07-02 12:02:15      阅读:70      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>商品sku</title>
</head>

<body>
    <script>
        const names = [‘iphoneX‘, ‘iphoneXs‘];
        const colors = [‘黑色‘, ‘白色‘];
        const sizes = [‘64G‘, ‘128G‘];
        // const res = [
        //     [‘iphoneX‘, ‘黑色‘, ‘64G‘],
        //     [‘iphoneX‘, ‘黑色‘, ‘128G‘],
        //     [‘iphoneX‘, ‘白色‘, ‘64G‘],
        //     [‘iphoneX‘, ‘白色‘, ‘128G‘],
        //     [‘iphoneXs‘, ‘黑色‘, ‘64G‘],
        //     [‘iphoneXs‘, ‘黑色‘, ‘128G‘],
        //     [‘iphoneXs‘, ‘白色‘, ‘64G‘],
        //     [‘iphoneXs‘, ‘白色‘, ‘128G‘]
        // ]
        const all = [names, colors, sizes]
        let combine = function (all) {
            if (all.length < 2) {
                return all[0] || []
            }
            return all.reduce((pre, cur) => {
                let res = []
                pre.forEach(p => {
                    cur.forEach(c => {
                        let t = [].concat(Array.isArray(p) ? p : [p])
                        t.push(c)
                        res.push(t)
                    });
                });
                return res
            })
        }
        // let combine = function (all) {
        //     let res = []
        //     let fn = function (itemIndex, prev) {
        //         let item = all[itemIndex]
        //         let isLast = itemIndex === all.length - 1
        //         for (const val of item) {
        //             let cur = prev.concat(val)
        //             if (isLast) {
        //                 res.push(cur)
        //             } else {
        //                 fn(itemIndex + 1, cur)
        //             }
        //         }
        //     }
        //     fn(0, [])
        //     return res
        // }
        console.log(combine(all));

    </script>
</body>

</html>

商品sku的排列组合

原文:https://www.cnblogs.com/samsara-yx/p/13223554.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!