Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

有如下数组,我想转换格式为这样,怎么转换呢

这个数据格式

    let skuAttrList = [{
            "attrName": "颜色",
            "attrValList": [{
                "skuValId": "123",
                "skuVal": "红色"
            }, {
                "skuValId": "456",
                "skuVal": "黑色"
            }]
        }, {
            "attrName": "尺寸",
            "attrValList": [{
                "skuValId": "789",
                "skuVal": "200ml"
            }, {
                "skuValId": "1011",
                "skuVal": "500ml"
            }]
        }];

我现在转换为

s1 等于颜色的id,因为尺寸有多个,所以一个s1两个s2

let newList = [
            {"s1":"123","s2":"789"},//s1颜色第一个id s2尺寸第一个id
            {"s1":"123","s2":"1011"},//s1颜色第一个id s2尺寸第二个id
            {"s1":"456","s2":"789"},//s1颜色第二个id s2尺寸id
            {"s1":"456","s2":"1011"}//s1颜色第二个id s2尺寸id
        ]

skuAttrList是动态的可能会多个,如果skuAttrList数组是

let skuAttrList = [
{
        "attrName": "颜色",
        "attrValList": [{
            "skuValId": "123",
            "skuVal": "红色"
        }, {
            "skuValId": "456",
            "skuVal": "黑色"
        }]
    }, 
    {
        "attrName": "尺寸",
        "attrValList": [{
            "skuValId": "789",
            "skuVal": "200ml"
        }, {
            "skuValId": "1011",
            "skuVal": "500ml"
        }]
    },
     {
        "attrName": "外观",
        "attrValList": [{
            "skuValId": "789",
            "skuVal": "奢华"
        }, {
            "skuValId": "1011",
            "skuVal": "中等"
        },
        {
            "skuValId": "1011",
            "skuVal": "普通"
        }
        ]
    },
    ];

那么对应的就要加上 s3 这个属性


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

1 Reply

0 votes
by (71.8m points)

多数组的组合。强撸:

function getPath(skuAttrList) {
    var result = null;
    for(let i = skuAttrList.length - 1; i >= 0; --i) {
        const { attrValList } = skuAttrList[i];      
        const currentNodePathList = attrValList.reduce((currentNodePathList, attrVal) => {
            const { skuValId } = attrVal
            currentNodePathList.push({ [`s${i + 1}`]:  skuValId});
            return currentNodePathList;
        }, []);

        if(!result) {
            result = currentNodePathList;
        } else {
            const tempResult = [];
            currentNodePathList.forEach(path => {
                result.forEach(preNodePath => {                    
                    tempResult.push({ ...path, ...preNodePath});
                })
            });
            result = tempResult;
        }       
    }
    return result
}

// TEST
getPath(skuAttrList);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...