以下是JSON数据记录: 结果=
[{
"name": "UserManagement",
"parent": "modules",
"grandpa": "non",
"level": "module",
"partitions": {
}
},
{
"name": "UsersRoles",
"level": "partition",
"parent": "UserManagement",
"grandpa": "modules",
"usecases": {
}
},
{
"name": "Login",
"level": "usecase",
"parent": "UsersRoles",
"grandpa": "UserManagement",
},
{
"name": "EditProfile",
"level": "usecase",
"parent": "UsersRoles",
"grandpa": "UserManagement",
},
{
"name": "UserActivities",
"level": "usecase",
"parent": "AuditTrail",
"grandpa": "UserManagement",
},
{
"name": "NewFiscalRecord",
"level": "usecase",
"parent": "AccountsAndFiscalRecords",
"grandpa": "Accounting",
},
{
"name": "FiscalReports",
"level": "partition",
"parent": "Accounting",
"grandpa": "modules",
"usecases": {
}
}]
我使用了很多库来对它们进行分组,例如lodash
:
https://jsfiddle.net/dqgceaxb/
var lodash = function (seq, keys) {
//console.log(keys.length)
if (!keys.length)
return seq;
var first = keys[0];
var rest = keys.slice(1);
return _.mapValues(_.groupBy(seq, first), function (value) {
return lodash(value, rest)
});
};
var res = lodash(result, ['grandpa','parent']);
和alasql
:
var json2group = function (input, attr) {
var res1 = alasql('SELECT ' + attr[0] + ' AS label0,' + attr[1] + ' AS label1,' +
' ARRAY({label:' + attr[0] + ',groups:_}) AS groups1 FROM ? GROUP BY label0,label1', [input]);
var res2 = alasql('SELECT label0 AS label,' +
' ARRAY({label:label1,groups:groups1}) AS groups FROM ? GROUP BY label', [res1]);
return res2;
};
var res = json2group(result, ["grandpa","parent"]);
以及grandpa
和parent
两种方式分组后的最终结果是:
{
"non": {
"modules": [
{
"name": "UserManagement",
"parent": "modules",
"grandpa": "non",
"level": "module",
"partitions": {
}
}
]
},
"modules": {
"UserManagement": [
{
"name": "UsersRoles",
"level": "partition",
"parent": "UserManagement",
"grandpa": "modules",
"usecases": {
}
}
],
"Accounting": [
{
"name": "FiscalReports",
"level": "partition",
"parent": "Accounting",
"grandpa": "modules",
"usecases": {
}
}
]
},
"UserManagement": {
"UsersRoles": [
{
"name": "Login",
"level": "usecase",
"parent": "UsersRoles",
"grandpa": "UserManagement",
},
{
"name": "EditProfile",
"level": "usecase",
"parent": "UsersRoles",
"grandpa": "UserManagement",
}
],
"AuditTrail": [
{
"name": "UserActivities",
"level": "usecase",
"parent": "AuditTrail",
"grandpa": "UserManagement",
}
]
},
"Accounting": {
"AccountsAndFiscalRecords": [
{
"name": "NewFiscalRecord",
"level": "usecase",
"parent": "AccountsAndFiscalRecords",
"grandpa": "Accounting",
}
]
}
}
但我希望结果如下:
{
"non": {
"modules": [
"UserManagement":
[
{
"name": "UserManagement",
"parent": "modules",
"grandpa": "non",
"level": "module",
"partitions": {
}
},
{
"UsersRoles":[
"name": "UsersRoles",
"level": "partition",
"parent": "UserManagement",
"grandpa": "modules",
"usecases": {
},
{
"name": "Login",
"level": "usecase",
"parent": "UsersRoles",
"grandpa": "UserManagement",
},
{
"name": "EditProfile",
"level": "usecase",
"parent": "UsersRoles",
"grandpa": "UserManagement",
}
]
},
{
"AuditTrail": [
{
"name": "UserActivities",
"level": "usecase",
"parent": "AuditTrail",
"grandpa": "UserManagement",
}
]
}
],
"Accounting": [
{
"name": "FiscalReports",
"level": "partition",
"parent": "Accounting",
"grandpa": "modules",
"usecases": {
}
},
{
"AccountsAndFiscalRecords": [
{
"name": "NewFiscalRecord",
"level": "usecase",
"parent": "AccountsAndFiscalRecords",
"grandpa": "Accounting",
}
]
}
]
]
}
有没有办法得到我想要的结果?怎么做? PS:如果需要,我可以向JSON对象添加属性。