For the implementation of commodity sku array, it is best to use reduce

original array, array is not fixed, there may be multiple, id and name are different, sid can be the same (the same belongs to the same category)

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" }
]

result composition

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" }] },
]

if

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" }] },
]
Mar.03,2021

reassembles your object according to the current object (it is the same even if written in reduce, or in the form of a pure function), which means pulling out one like sid, and then reassembling one object into another array. That's the general idea


after the boss's guidance, I understand that this is a Cartesian product, and refer to the Cartesian product algorithm
the parsing of this question is as follows. The code is a bit messy. Please forgive me

.
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))
Menu