我有一个小型的Web应用程序,该应用程序使用API调用来获取数据,响应是报告数组,每个报告都有唯一的ID,应用程序,类型和标题。
我想通过按应用程序分组然后按描述如下的类型将其映射到新对象中。
[
{
"id": "REPORT1",
"application": "APP1",
"type": "TYPE1",
"title": ""
},
{
"id": "REPORT2",
"application": "APP1",
"type": "TYPE1",
"title": ""
},
{
"id": "REPORT3",
"application": "APP1",
"type": "TYPE2",
"title": ""
},
{
"id": "REPORT4",
"application": "APP2",
"type": "TYPE3",
"title": ""
}
]
Run Code Online (Sandbox Code Playgroud)
{
"APP1": {
"TYPE1": [
{
"id": "REPORT1",
"application": "APP1",
"type": "TYPE1",
"title": ""
},
{
"id": "REPORT2",
"application": "APP1",
"type": "TYPE1",
"title": ""
}
],
"TYPE2": [
{
"id": "REPORT3",
"application": "APP1",
"type": "TYPE2",
"title": ""
}
]
},
"APP2": {
"TYPE3": [
{
"id": "REPORT4",
"application": "APP2",
"type": "TYPE3",
"title": ""
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
随着loadash我来到了这一点:
const output = _(input)
.groupBy(report => report.application)
.value()
Run Code Online (Sandbox Code Playgroud)
分组依据之后application,我需要进行另一个嵌套分组或映射,type但是卡住了。
这可以很容易地通过没有被lodash实现的Array#reduce()功能。
有关如何实现此功能的详细信息,请参见代码片段源代码中的注释:
var input = [{
"id": "REPORT1",
"application": "APP1",
"type": "TYPE1",
"title": ""
},
{
"id": "REPORT2",
"application": "APP1",
"type": "TYPE1",
"title": ""
},
{
"id": "REPORT3",
"application": "APP1",
"type": "TYPE2",
"title": ""
},
{
"id": "REPORT4",
"application": "APP2",
"type": "TYPE3",
"title": ""
}
];
var output = input.reduce((result, item) => {
// Get app object corresponding to current item from result (or insert if not present)
var app = result[item.application] = result[item.application] || {};
// Get type array corresponding to current item from app object (or insert if not present)
var type = app[item.type] = app[item.type] || [];
// Add current item to current type array
type.push(item);
// Return the result object for this iteration
return result;
}, {});
console.log(output);Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper {
height:100% !important;
max-height:unset !important;
}Run Code Online (Sandbox Code Playgroud)
您可以采用动态方法,使用所需的键进行分组。
Run Code Online (Sandbox Code Playgroud)groups = ["application", "type"]
然后减少数据数组和键数组,并根据需要构建新对象,或者使用最后一组构建用于推送实际对象的数组。
如有必要,该解决方案可以轻松扩展到更多嵌套组。
groups = ["application", "type"]
Run Code Online (Sandbox Code Playgroud)
var data = [{ id: "REPORT1", application: "APP1", type: "TYPE1", title: "" }, { id: "REPORT2", application: "APP1", type: "TYPE1", title: "" }, { id: "REPORT3", application: "APP1", type: "TYPE2", title: "" }, { id: "REPORT4", application: "APP2", type: "TYPE3", title: "" }],
groups = ["application", "type"],
grouped = data.reduce((r, o) => {
groups
.reduce((group, key, i, { length }) =>
group[o[key]] = group[o[key]] || (i + 1 === length ? [] : {}), r)
.push(o);
return r;
}, {});
console.log(grouped);Run Code Online (Sandbox Code Playgroud)
es6,object spread and reduce 让它变得如此简单
const input = [
{
"id": "REPORT1",
"application": "APP1",
"type": "TYPE1",
"title": ""
},
{
"id": "REPORT2",
"application": "APP1",
"type": "TYPE1",
"title": ""
},
{
"id": "REPORT3",
"application": "APP1",
"type": "TYPE2",
"title": ""
},
{
"id": "REPORT4",
"application": "APP2",
"type": "TYPE3",
"title": ""
}
]
const output = input.reduce((acc, item) => ({
...acc,
[item.application]: {
...acc[item.application],
[item.type]: [
...(acc[item.application] && acc[item.application][item.type] || []),
item,
]
}
}), {})
console.log(output)Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1183 次 |
| 最近记录: |