1

以下是 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": {
    }
}]

我使用了许多库对它们进行分组,例如lodashhttps ://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"]);

grandpaparent两种方式分组后的最终结果是:

{
"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 对象添加属性。

4

0 回答 0