Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

现在有如下数据格式,我想转换为这样

let skuList = [
            {
            "stock": 10,
            "stockWarningValue": 2,
            "attrValList": [{
                "skuKeyId": "712957877970780160",
                "skuKey": "颜色",
                "skuValId": "123",
                "val": "红色"
            }, {
                "skuKeyId": "712958011827798016",
                "skuKey": "尺寸",
                "skuValId": "1011",
                "val": "500ml"
            }]
        }, 
        {
            
            "stock": 200,
            "stockWarningValue": 2,
            "attrValList": [{
                "skuKeyId": "712957877970780160",
                "skuKey": "颜色",
                "skuValId": "123",
                "val": "红色"
            }, {
                "skuKeyId": "712958011827798016",
                "skuKey": "尺寸",
                "skuValId": "789",
                "val": "200ml"
            }],
        }, 
        {
            "stock": 20,
            "stockWarningValue": 2,
            "attrValList": [{
                "skuKeyId": "712957877970780160",
                "skuKey": "颜色",
                "skuValId": "456",
                "val": "黑色"
            }, {
                "skuKeyId": "712958011827798016",
                "skuKey": "尺寸",
                "skuValId": "1011",
                "val": "500ml"
            }],
        }, 
        {
            "stock": 200,
            "stockWarningValue": 2,
            "attrValList": [{
                "skuKeyId": "712957877970780160",
                "skuKey": "颜色",
                "skuValId": "456",
                "val": "黑色"
            }, {
                "skuKeyId": "712958011827798016",
                "skuKey": "尺寸",
                "skuValId": "789",
                "val": "200ml"
            }],
        },
        ]

我想转换为

        let skuAttrList = [{
            "attrName": "颜色",//这里面是所有的颜色
            "attrValList": [{
                "skuValId": "123",
                "skuVal": "红色"
            }, {
                "skuValId": "456",
                "skuVal": "黑色"
            }]
        }, {
            "attrName": "尺寸",//这里面是所有的尺寸
            "attrValList": [{
                "skuValId": "789",
                "skuVal": "200ml"
            }, {
                "skuValId": "1011",
                "skuVal": "500ml"
            }]
        }],

这个skuList 是动态的
如果skuList为

let skuList = [
            {
            "stock": 10,
            "stockWarningValue": 2,
            "attrValList": [{
                "skuKeyId": "712957877970780160",
                "skuKey": "颜色",
                "skuValId": "123",
                "val": "红色"
            }, {
                "skuKeyId": "712958011827798016",
                "skuKey": "尺寸",
                "skuValId": "1011",
                "val": "500ml"
            },
            {//新增的对象
                "skuKeyId": "545412",
                "skuKey": "品牌",
                "skuValId": "965",
                "val": "奢华"
            },
            ]
        }, 
        {
            
            "stock": 200,
            "stockWarningValue": 2,
            "attrValList": [{
                "skuKeyId": "712957877970780160",
                "skuKey": "颜色",
                "skuValId": "123",
                "val": "红色"
            }, {
                "skuKeyId": "712958011827798016",
                "skuKey": "尺寸",
                "skuValId": "789",
                "val": "200ml"
            },
             {//新增的对象
                "skuKeyId": "545412",
                "skuKey": "品牌",
                "skuValId": "1545",
                "val": "普通"
            },
            ],
        }, 
        {
            "stock": 20,
            "stockWarningValue": 2,
            "attrValList": [{
                "skuKeyId": "712957877970780160",
                "skuKey": "颜色",
                "skuValId": "456",
                "val": "黑色"
            }, {
                "skuKeyId": "712958011827798016",
                "skuKey": "尺寸",
                "skuValId": "1011",
                "val": "500ml"
            },
              {//新增的对象
                "skuKeyId": "545412",
                "skuKey": "品牌",
                "skuValId": "564",
                "val": "普通"
            },
            ],
        }, 
        {
            "stock": 200,
            "stockWarningValue": 2,
            "attrValList": [{
                "skuKeyId": "712957877970780160",
                "skuKey": "颜色",
                "skuValId": "456",
                "val": "黑色"
            }, {
                "skuKeyId": "712958011827798016",
                "skuKey": "尺寸",
                "skuValId": "789",
                "val": "200ml"
            },
             {//新增的对象
                "skuKeyId": "545412",
                "skuKey": "品牌",
                "skuValId": "212",
                "val": "vip"
            },
            ],
        },
        ]

那么 skuAttrList 对应的也要加入 品牌的


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Object.entries(skuList.reduce((res, sku) => {
    sku.attrValList.forEach(v => {
        res[v.skuKey] = res[v.skuKey]||[];
        if(res[v.skuKey].every(o => o.skuValId != v.skuValId)) {
            res[v.skuKey].push({skuValId: v.skuValId, skuVal: v.val})
        }
    })
    return res;
}, {})).map(([attrName, attrValList]) => ({attrName, attrValList}))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...