我有一个平面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();
}
}...gets分组权限,但删除每个组的密钥:
{
"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。
发布于 2019-04-27 04:59:14
此解决方案适用于ES5和lodash 3.10.1。它基于Nina Scholz's answer中的道具思想。
然而,这里的想法并不是从end对象中删除属性,而是让每个级别在group之后选择自己的属性,如果是最后一个级别,则避免分组,并从属性创建一个数组。
function nest(collection, props) {
var currentProps = _.first(props);
var restProps = _.rest(props);
if(_.size(props) === 1) {
return _.map(collection, _.partialRight(_.pick, currentProps));
}
return _(collection)
.groupBy(_.first(currentProps))
.map(function(values) {
var item = _.first(values);
var fields = _.pick(item, _.initial(currentProps));
var collectionKey = _.last(currentProps);
fields[collectionKey] = nest(values, restProps);
return fields;
})
.value();
}
var myCatalog = {"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"}]}]}};
var props = [["pagenumber", "versions"], ["pageversion", "category", "pagetype", "partners"], ["PtrChild", "allocations"], ["Allocation", "ads"], ["catext"]];
var myGroupedData = nest(_.flatten(_.map(myCatalog.dataSet.dataTable, 'row')), props);
console.log(myGroupedData);<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
发布于 2019-04-27 04:15:00
您可以为每个级别获取所需属性的帮助器数组,为不同命名的子数组获取最后一项,并使用一个方法提取对象的已用属性,以获得最终推送的清理对象。
var data = { 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" }] }] } },
props = [["pagenumber", "versions"], ["pageversion", "category", "pagetype", "partners"], ["PtrChild", "allocations"], ["Allocation", "ads"]],
result = data.dataSet.dataTable.reduce((r, { row }) => {
row.forEach(o => props.reduce((a, [...keys]) => {
var children = keys.pop(),
temp = a.find(p => p[keys[0]] === o[keys[0]]);
if (!temp) a.push(temp = Object.assign(...keys.map(k => ({ [k]: o[k] })), { [children]: [] }));
o = keys.reduce((p, k) => ({ [k]: k, ...p } = p, p), o);
return temp[children];
}, r).push(o));
return r;
}, []);
console.log(result);.as-console-wrapper { max-height: 100% !important; top: 0; }
https://stackoverflow.com/questions/55873913
复制相似问题