通过多个键对对象数组进行分组和展平

时间:2019-05-10 07:53:38

标签: javascript arrays lodash

我正在尝试对以下对象数组进行分组和展平:

const data = [
            { RoleId: 1, RoleName: 'Admin', Permissions: 1 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 38 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 4 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 18 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 4001 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 30 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 20 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 2 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 10 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 99 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 5 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 22 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 4002 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 34 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 32 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 3 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 14 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 6 },
            { RoleId: 1, RoleName: 'Admin', Permissions: 26 },
            { RoleId: 2, RoleName: 'AgencyAdmin', Permissions: 10 },
            { RoleId: 2, RoleName: 'AgencyAdmin', Permissions: 3 },
            { RoleId: 2, RoleName: 'AgencyAdmin', Permissions: 4001 },
            { RoleId: 2, RoleName: 'AgencyAdmin', Permissions: 18 },
            { RoleId: 2, RoleName: 'AgencyAdmin', Permissions: 4002 },
            { RoleId: 2, RoleName: 'AgencyAdmin', Permissions: 30 },
            { RoleId: 2, RoleName: 'AgencyAdmin', Permissions: 2 },
            { RoleId: 3, RoleName: 'InstituteAdmin', Permissions: 30 },
            { RoleId: 3, RoleName: 'InstituteAdmin', Permissions: 6 },
            { RoleId: 3, RoleName: 'InstituteAdmin', Permissions: 10 },
            { RoleId: 3, RoleName: 'InstituteAdmin', Permissions: 2 },
            { RoleId: 3, RoleName: 'InstituteAdmin', Permissions: 4002 },
            { RoleId: 3, RoleName: 'InstituteAdmin', Permissions: 18 },
            { RoleId: 3, RoleName: 'InstituteAdmin', Permissions: 3 },
            { RoleId: 3, RoleName: 'InstituteAdmin', Permissions: 4001 }
        ];

我尝试使用lodash的groupBy方法,但无法按多个键进行分组,也尝试使用zipObject,但无法获得预期的结果

groupBy(data, 'RoleId')

为我提供了一个按RoleId分组的数组,但使用键数组却无法使用

我希望结果数组看起来像如下:

[
{RoleId: 1, RoleName:'Admin', Permissions: [1, 38, 4, 18, ...]},
{RoleId: 2, RoleName:'AgencyAdmin', Permissions: [1, 38, 4, 18, ...]},
{RoleId: 3, RoleName:'InstituteAdmin', Permissions: [1, 38, 4, 18, ...]},
]

有人知道我该如何解决这个问题?任何帮助,将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

使用reduce

const data = [{"RoleId":1,"RoleName":"Admin","Permissions":1},{"RoleId":1,"RoleName":"Admin","Permissions":38},{"RoleId":1,"RoleName":"Admin","Permissions":4},{"RoleId":1,"RoleName":"Admin","Permissions":18},{"RoleId":1,"RoleName":"Admin","Permissions":4001},{"RoleId":1,"RoleName":"Admin","Permissions":30},{"RoleId":1,"RoleName":"Admin","Permissions":20},{"RoleId":1,"RoleName":"Admin","Permissions":2},{"RoleId":1,"RoleName":"Admin","Permissions":10},{"RoleId":1,"RoleName":"Admin","Permissions":99},{"RoleId":1,"RoleName":"Admin","Permissions":5},{"RoleId":1,"RoleName":"Admin","Permissions":22},{"RoleId":1,"RoleName":"Admin","Permissions":4002},{"RoleId":1,"RoleName":"Admin","Permissions":34},{"RoleId":1,"RoleName":"Admin","Permissions":32},{"RoleId":1,"RoleName":"Admin","Permissions":3},{"RoleId":1,"RoleName":"Admin","Permissions":14},{"RoleId":1,"RoleName":"Admin","Permissions":6},{"RoleId":1,"RoleName":"Admin","Permissions":26},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":10},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":3},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":4001},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":18},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":4002},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":30},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":2},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":30},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":6},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":10},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":2},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":4002},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":18},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":3},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":4001}];

// Iterate over `data` (acc is the accumulated output (an object)
// Note that because the accumulator output is an object
// we need to pick out its values to create the final output
const out = Object.values(data.reduce((acc, current) => {

  // Destructure the properies from the current object
  const { RoleId, Permissions, ...rest } = current;

  // If the object doesn't already contain a RoleId key set up a new object with an
  // empty permissions array
  acc[RoleId] = acc[RoleId] || { RoleId, Permissions: [], ...rest };

  // Push the permissions of the current object to the permissions array
  acc[RoleId].Permissions.push(Permissions);

  // Return the accumulator for the next iteration
  return acc;
}, {}));

console.log(out);

答案 1 :(得分:0)

您可以先按RoleId分组,然后循环遍历各个组以构造权限集合。这是一个示例:

const data = [{"RoleId":1,"RoleName":"Admin","Permissions":1},{"RoleId":1,"RoleName":"Admin","Permissions":38},{"RoleId":1,"RoleName":"Admin","Permissions":4},{"RoleId":1,"RoleName":"Admin","Permissions":18},{"RoleId":1,"RoleName":"Admin","Permissions":4001},{"RoleId":1,"RoleName":"Admin","Permissions":30},{"RoleId":1,"RoleName":"Admin","Permissions":20},{"RoleId":1,"RoleName":"Admin","Permissions":2},{"RoleId":1,"RoleName":"Admin","Permissions":10},{"RoleId":1,"RoleName":"Admin","Permissions":99},{"RoleId":1,"RoleName":"Admin","Permissions":5},{"RoleId":1,"RoleName":"Admin","Permissions":22},{"RoleId":1,"RoleName":"Admin","Permissions":4002},{"RoleId":1,"RoleName":"Admin","Permissions":34},{"RoleId":1,"RoleName":"Admin","Permissions":32},{"RoleId":1,"RoleName":"Admin","Permissions":3},{"RoleId":1,"RoleName":"Admin","Permissions":14},{"RoleId":1,"RoleName":"Admin","Permissions":6},{"RoleId":1,"RoleName":"Admin","Permissions":26},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":10},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":3},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":4001},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":18},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":4002},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":30},{"RoleId":2,"RoleName":"AgencyAdmin","Permissions":2},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":30},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":6},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":10},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":2},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":4002},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":18},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":3},{"RoleId":3,"RoleName":"InstituteAdmin","Permissions":4001}];
var groups = _.groupBy(data, 'RoleId');

var result = _.keys(groups).map(k => {
  return {
    ..._.pick(groups[k][0], ['RoleId', 'RoleName']),
    Permissions: _.map(groups[k], 'Permissions')
  }
});

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>