商品sku数组的实现,最好是用reduce

原始数组,数组不固定,可能有多个,id和name不同,sid可以相同(相同属于同一类)

var arr = [
    { "id": "1", "sid": "mm", "name": "黑色" },
    { "id": "2", "sid": "mm", "name": "白色" },
    { "id": "3", "sid": "xx", "name": "64G" },
    { "id": "4", "sid": "xx", "name": "128G" },
    { "id": "5", "sid": "yy", "name": "normal" },
    { "id": "6", "sid": "yy", "name": "plus" }
]

结果组成

var arr2 = [
    { name: '黑色,64G,normal', attrs: [{ "id": "1", "sid": "mm", "name": "黑色" }, { "id": "3", "sid": "xx", "name": "64G" }, { "id": "5", "sid": "yy", "name": "normal" }] },
    { name: '黑色,64G,plus', attrs: [{ "id": "1", "sid": "mm", "name": "黑色" }, { "id": "3", "sid": "xx", "name": "64G" }, { "id": "6", "sid": "yy", "name": "plus" }] },
    { name: '黑色,128G,normal', attrs: [{ "id": "1", "sid": "mm", "name": "黑色" }, { "id": "4", "sid": "xx", "name": "128G" }, { "id": "5", "sid": "yy", "name": "normal" }] },
    { name: '黑色,128G,plus', attrs: [{ "id": "1", "sid": "mm", "name": "黑色" }, { "id": "4", "sid": "xx", "name": "128G" }, { "id": "6", "sid": "yy", "name": "plus" }] },
    { name: '白色,64G,normal', attrs: [{ "id": "2", "sid": "mm", "name": "白色" }, { "id": "3", "sid": "xx", "name": "64G" }, { "id": "5", "sid": "yy", "name": "normal" }] },
    { name: '白色,64G,plus', attrs: [{ "id": "2", "sid": "mm", "name": "白色" }, { "id": "3", "sid": "xx", "name": "64G" }, { "id": "6", "sid": "yy", "name": "plus" }] },
    { name: '白色,128G,normal', attrs: [{ "id": "2", "sid": "mm", "name": "白色" }, { "id": "4", "sid": "xx", "name": "128G" }, { "id": "5", "sid": "yy", "name": "normal" }] },
    { name: '白色,128G,plus', attrs: [{ "id": "2", "sid": "mm", "name": "白色" }, { "id": "4", "sid": "xx", "name": "128G" }, { "id": "6", "sid": "yy", "name": "plus" }] },
]

如果

var arr = [
    { "id": "1", "sid": "mm", "name": "黑色" },
    { "id": "2", "sid": "mm", "name": "白色" },
    { "id": "3", "sid": "xx", "name": "64G" },
    { "id": "4", "sid": "xx", "name": "128G" }
    ]
var arr2 = [
    { name: '黑色,64G', attrs: [{ "id": "1", "sid": "mm", "name": "黑色" }, { "id": "3", "sid": "xx", "name": "64G" }] },
    { name: '黑色,128G', attrs: [{ "id": "1", "sid": "mm", "name": "黑色" }, { "id": "4", "sid": "xx", "name": "128G" }] },
    { name: '白色,64G', attrs: [{ "id": "2", "sid": "mm", "name": "白色" }, { "id": "3", "sid": "xx", "name": "64G" }] },
    { name: '白色,128G', attrs: [{ "id": "2", "sid": "mm", "name": "白色" }, { "id": "4", "sid": "xx", "name": "128G" }] },
]
阅读 799
评论
    2 个回答
    • 388

    经过大佬指点,了解了这是笛卡尔积,参考了笛卡尔积算法
    本题的解析如下,代码有点乱,请谅解

    function descartes() {
        let newObj = {}
        let newArr = [];
        arr.forEach((item, index) => {
            if (newObj.hasOwnProperty(item.sid)) {
                newObj[item.sid].push(item)
            } else {
                newObj[item.sid] = [];
                newObj[item.sid].push(item);
            }
        })
        newArr = Object.values(newObj);
        let arr1;
        arr1 = newArr[0].map((item) => {
            return { name: item.name, attrs: [item] }
        })
        newArr[0] = arr1;
        if (newArr.length === 1) {
            return arr1;
        } else {
            return newArr.reduce((col, set) => {
                let res = [];
                col.forEach((c) => {
                    set.forEach((s) => {
                        let t = { name:"", attrs: [] }
                        t.attrs = t.attrs.concat(c.attrs);
                        t.attrs.push(s);
                        t.name = c.name + s.name;
                        res.push(t);
                    })
                });
                return res;
            });
        }
    }
    let arr2 = descartes();
    console.log(JSON.stringify(arr2))
    

      按照当前重组对象(就算写在reduce也是一样的,还是纯函数的形式重组你的对象),等于说就是sid一样的抽一个出来,然后重新组成一个对象放到另外一个数组里。大致思路就这样

        撰写回答

        登录后参与交流、获取后续更新提醒