Javascript 在多个键上分组JSON数据并保留键

Javascript 在多个键上分组JSON数据并保留键,javascript,lodash,Javascript,Lodash,我有一个平面JSON文件,如下所示: {"dataSet": {"dataTable": [{ "id": "List1", "row": [ { "pagenumber": "7", "pageversion": "HE; K12", "category": "3D Print - K12;HE", "pagetype": "Product",

我有一个平面JSON文件,如下所示:

{"dataSet": {"dataTable": [{
    "id": "List1",
    "row": [
        {
            "pagenumber": "7",
            "pageversion": "HE; K12",
            "category": "3D Print - K12;HE",
            "pagetype": "Product",
            "PtrChild": "MakerBot - 10771",
            "Allocation": "0.500",
            "catext": "Text goes here"
        },
        {
            "pagenumber": "7",
            "pageversion": "SL",
            "category": "3D Print - SL",
            "pagetype": "Product",
            "PtrChild": "AUTODESK - 10032",
            "Allocation": "0.500",
            "catext": "Text goes here"
        },
        {
            "pagenumber": "10",
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Brand",
            "PtrChild": null,
            "Allocation": "1.000",
            "catext": "Text goes here"
        },
        {
            "pagenumber": "11",
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Product",
            "PtrChild": "EPSON INK JET** - 10082",
            "Allocation": "0.200",
            "catext": "Text goes here"
        },
        {
            "pagenumber": "11",
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Product",
            "PtrChild": "EPSON INK JET** - 10082",
            "Allocation": "0.200",
            "catext": "Text goes here"
        },
        {
            "pagenumber": "11",
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Product",
            "PtrChild": "EPSON INK JET** - 10082",
            "Allocation": "0.500",
            "catext": "Text goes here"
        },
        {
            "pagenumber": "11",
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Product",
            "PtrChild": "LEXMARK** - 10151",
            "Allocation": "0.200",
            "catext": "Text goes here"
        }
    ]
}]}}
我需要按pagenumber、pageversion、PtrChild、Allocation对数据进行分组,以便在完成时如下所示:

{"pages": [
    {
        "pagenumber": "7",
        "versions": [
            {
                "pageversion": "HE; K12",
                "category": "3D Print - K12;HE",
                "pagetype": "Product",
                "partners": [{
                    "PtrChild": "MakerBot - 10771",
                    "allocations": [{
                        "Allocation": "0.500",
                        "ads": [{"catext": "Text goes here"}]
                    }]
                }]
            },
            {
                "pageversion": "SL",
                "category": "3D Print - SL",
                "pagetype": "Product",
                "partners": [{
                    "PtrChild": "AUTODESK - 10032",
                    "allocations": [{
                        "Allocation": "0.500",
                        "ads": [{"catext": "Text goes here"}]
                    }]
                }]
            }
        ]
    },
    {
        "pagenumber": "10",
        "versions": [{
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Brand",
            "partners": [{
                "PtrChild": null,
                "allocations": [{
                    "Allocation": "1.500",
                    "ads": [{"catext": "Text goes here"}]
                }]
            }]
        }]
    },
    {
        "pagenumber": "11",
        "versions": [{
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Product",
            "partners": [
                {
                    "PtrChild": "EPSON INK JET** - 10082",
                    "allocations": [
                        {
                            "Allocation": "0.250",
                            "ads": [
                                {"catext": "Text goes here"},
                                {"catext": "Text goes here"}
                            ]
                        },
                        {
                            "Allocation": "0.500",
                            "ads": [{"catext": "Text goes here"}]
                        }
                    ]
                },
                {
                    "PtrChild": "LEXMARK** - 10151",
                    "allocations": [{
                        "Allocation": "0.200",
                        "ads": [{"catext": "Text goes here"}]
                    }]
                }
            ]
        }]
    }
]}
运行此:

var myGroupedData = nest(myCatalog, ["pagenumber", "pageversion", "PtrChild", "Allocation"]);
// Reorganize JSON data
function nest(collection, keys) {
    if (!keys.length) {
        return collection;
    }
    else {
        return _(collection).groupBy(keys[0]).mapValues(function(values) { 
            return nest(values, keys.slice(1));
        }).value();
    }
}
…获得正确的分组,但消除每个组的键:

{
    "7": {
        "HE; K12": {"MakerBot - 10771": {"0.500": [{
            "pagenumber": "7",
            "pageversion": "HE; K12",
            "category": "3D Print - K12;HE",
            "pagetype": "Product",
            "PtrChild": "MakerBot - 10771",
            "Allocation": "0.500",
            "catext": "Text goes here"
        }]}},
        "SL": {"AUTODESK - 10032": {"0.500": [{
            "pagenumber": "7",
            "pageversion": "SL",
            "category": "3D Print - SL",
            "pagetype": "Product",
            "PtrChild": "AUTODESK - 10032",
            "Allocation": "0.500",
            "catext": "Text goes here"
        }]}}
    },
    "10": {"Apply to All": {"null": {"1.000": [{
        "pagenumber": "10",
        "pageversion": "Apply to All",
        "category": "Secure Printers",
        "pagetype": "Brand",
        "PtrChild": null,
        "Allocation": "1.000",
        "catext": "Text goes here"
    }]}}},
    "11": {"Apply to All": {
        "EPSON INK JET** - 10082": {
            "0.200": [
                {
                    "pagenumber": "11",
                    "pageversion": "Apply to All",
                    "category": "Secure Printers",
                    "pagetype": "Product",
                    "PtrChild": "EPSON INK JET** - 10082",
                    "Allocation": "0.200",
                    "catext": "Text goes here"
                },
                {
                    "pagenumber": "11",
                    "pageversion": "Apply to All",
                    "category": "Secure Printers",
                    "pagetype": "Product",
                    "PtrChild": "EPSON INK JET** - 10082",
                    "Allocation": "0.200",
                    "catext": "Text goes here"
                }
            ],
            "0.500": [{
                "pagenumber": "11",
                "pageversion": "Apply to All",
                "category": "Secure Printers",
                "pagetype": "Product",
                "PtrChild": "EPSON INK JET** - 10082",
                "Allocation": "0.500",
                "catext": "Text goes here"
            }]
        },
        "LEXMARK** - 10151": {"0.200": [{
            "pagenumber": "11",
            "pageversion": "Apply to All",
            "category": "Secure Printers",
            "pagetype": "Product",
            "PtrChild": "LEXMARK** - 10151",
            "Allocation": "0.200",
            "catext": "Text goes here"
        }]}
    }}
}

如何保留关键点并在多个关键点上分组


我被迫使用ExtendScript,因此无法使用版本3.10.1以外的Lodash。

您可以为每个级别的所需属性获取一个帮助器数组,为不同的命名子数组获取最后一项,并使用一种方法提取对象的已用属性,以便在最终推送时获得已清理的对象

var data={dataSet:{dataTable:[{id:“List1”,行:[{pagenumber:“7”,pageversion:“HE;K12”,类别:“3D Print-K12;HE”,pagetype:“Product”,PtrChild:“MakerBot-10771”,分配:“0.500”,catext:“Text goes here”},{pagenumber:“7”,pageversion:“SL”,类别:“3D Print-SL”,pagetype:“Product”,PtrChild:“AUTODESK-10032”,分配:“0.500”,catext:“文本在此显示”},{pagenumber:“10”,pageversion:“适用于所有人”,类别:“安全打印机”,pagetype:“品牌”,PtrChild:null,分配:“1.000”,catext:“文本在此显示”},{pagenumber:“11”,pageversion:“适用于所有人”,类别:“安全打印机”,pagetype:“产品”,PtrChild:爱普生喷墨**-10082”,分配:“0.200”,类别:“文本到这里”},{pagenumber:“11”,pageversion:“适用于所有人”,类别:“安全打印机”,pagetype:“产品”,PtrChild:“爱普生喷墨**-10082”,分配:“0.200”,类别:“文本到这里”},{pagenumber:“11”,pageversion:“适用于所有人”,类别:“安全打印机”,pagetype::产品“,PtrChild:“爱普生喷墨**-10082”,分配:“0.500”,catext:“文本到此”},{pagenumber:“11”,pageversion:“适用于所有人”,类别:“安全打印机”,页面类型:“产品”,PtrChild:“LEXMARK**-10151”,分配:“0.200”,catext:“文本到此”},},
道具=[“页码”、“版本”]、[“页面版本”、“类别”、“页面类型”、“合作伙伴”]、[“PtrChild”、“分配”]、[“分配”、“广告”],
结果=data.dataSet.dataTable.reduce((r,{row})=>{
row.forEach(o=>props.reduce((a,[…键])=>{
var children=keys.pop(),
temp=a.find(p=>p[keys[0]]==o[keys[0]]);
如果(!temp)a.push(temp=Object.assign(…keys.map(k=>({[k]:o[k]})),{[children]:[]});
o=keys.reduce((p,k)=>({[k]:k,…p}=p,p),o);
返回临时[儿童];
}推(o));
返回r;
}, []);
console.log(结果);

.as console wrapper{max height:100%!important;top:0;}
您可以为每个级别的所需属性获取一个helper数组,为不同的命名子数组获取最后一项,以及一个用于提取对象的已用属性的方法,以便为最终推送获取已清理的对象

var data={dataSet:{dataTable:[{id:“List1”,行:[{pagenumber:“7”,pageversion:“HE;K12”,类别:“3D Print-K12;HE”,pagetype:“Product”,PtrChild:“MakerBot-10771”,分配:“0.500”,catext:“Text goes here”},{pagenumber:“7”,pageversion:“SL”,类别:“3D Print-SL”,pagetype:“Product”,PtrChild:“AUTODESK-10032”,分配:“0.500”,catext:“文本在此显示”},{pagenumber:“10”,pageversion:“适用于所有人”,类别:“安全打印机”,pagetype:“品牌”,PtrChild:null,分配:“1.000”,catext:“文本在此显示”},{pagenumber:“11”,pageversion:“适用于所有人”,类别:“安全打印机”,pagetype:“产品”,PtrChild:爱普生喷墨**-10082”,分配:“0.200”,类别:“文本到这里”},{pagenumber:“11”,pageversion:“适用于所有人”,类别:“安全打印机”,pagetype:“产品”,PtrChild:“爱普生喷墨**-10082”,分配:“0.200”,类别:“文本到这里”},{pagenumber:“11”,pageversion:“适用于所有人”,类别:“安全打印机”,pagetype::产品“,PtrChild:“爱普生喷墨**-10082”,分配:“0.500”,catext:“文本到此”},{pagenumber:“11”,pageversion:“适用于所有人”,类别:“安全打印机”,页面类型:“产品”,PtrChild:“LEXMARK**-10151”,分配:“0.200”,catext:“文本到此”},},
道具=[“页码”、“版本”]、[“页面版本”、“类别”、“页面类型”、“合作伙伴”]、[“PtrChild”、“分配”]、[“分配”、“广告”],
结果=data.dataSet.dataTable.reduce((r,{row})=>{
row.forEach(o=>props.reduce((a,[…键])=>{
var children=keys.pop(),
temp=a.find(p=>p[keys[0]]==o[keys[0]]);
如果(!temp)a.push(temp=Object.assign(…keys.map(k=>({[k]:o[k]})),{[children]:[]});
o=keys.reduce((p,k)=>({[k]:k,…p}=p,p),o);
返回临时[儿童];
}推(o));
返回r;
}, []);
console.log(结果);

.as console wrapper{max height:100%!important;top:0;}
此解决方案适用于ES5和lodash 3.10.1。它基于中的道具思想

但是,这里的想法不是从最终对象中删除属性,而是让每个级别在分组后拾取自己的属性,如果是最后一个级别,则避免分组,并从属性创建数组

函数嵌套(集合、道具){
var currentProps=u.first(props);
var restProps=u.rest(props);
如果(道具大小=1){
return u.map(collection,u.partOK(u.pick,currentProps));
}  
返回(集合)
.groupBy(u.first(currentProps))
.map(函数(值){
var项目=u0.first(值);
变量字段=u.pick(项,u.initial(currentProps));
var collectionKey=u.last(currentProps);
字段[collectionKey]=嵌套(值、restProps);
返回字段;
})
.value();
}
var myCatalog={“数据集”:{“数据表”:[{“id”:“Li”