首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在lodash中按多个字段分组和排序

在lodash中按多个字段分组和排序
EN

Stack Overflow用户
提问于 2021-07-15 22:55:24
回答 2查看 152关注 0票数 0

假设这个数组:

代码语言:javascript
复制
[
    { department: "D1", section: "S1", name: "Test", other: "value", hierarchy: 1 },
    { department: "D2", section: "S1", name: "Test", other: "value", hierarchy: 1 },
    { department: "D2", section: "S2", name: "Test", other: "value", hierarchy: 2 }
]

我想按部门、部门对数据进行分组,并对部门、部门、层次结构进行排序

结果将是:

代码语言:javascript
复制
[
  "D1": [
     "S1": [
       { name: "Test", other: "value" }
     ]
  ],
  "D2": [
     "S1" [
       { name: "Test", other: "value" }
     ],
     "S2" [
       { name: "Test", other: "value" },
       { name: "Test", other: "value" }
     ]
  ]
]

这是我前进方向的一个框架:

代码语言:javascript
复制
var grouped = _.mapValues(_.chain(data).sortBy(item => item.hierarchy).groupBy(item => item.department).value(),
                      clist => clist.map(data => _.omit(data, 'department')));

_.forEach(grouped, function (item, department) {
    const members = _.groupBy(item, (item) => {
         return [item['section'], item['name']];
    });
});

有没有更好的方法使用Lodash来实现它?

EN

回答 2

Stack Overflow用户

发布于 2021-07-15 23:03:42

我建议先使用_.groupBy,然后使用_.mapValues

groupBy调用将按部门分组,然后我们再次使用groupBy使用mapValues将每个部门按部门分组。

代码语言:javascript
复制
const arr = [
    { department: "D1", section: "S1", name: "Test", other: "value", hierarchy: 1 },
    { department: "D2", section: "S2", name: "Test", other: "value", hierarchy: 2 },
    { department: "D2", section: "S1", name: "Test", other: "value", hierarchy: 1 },
    { department: "D2", section: "S2", name: "Test", other: "value", hierarchy: 2 }
]
const result = _.mapValues(_.groupBy(arr, 'department'), (v,k) => _.groupBy(v, 'section'));

console.log('Result:', result);
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>

票数 1
EN

Stack Overflow用户

发布于 2021-07-15 23:05:20

您可以使用vanilla javascript中的sort()reduce()实现这一点。

代码语言:javascript
复制
const
  input = [
    { department: "D2", section: "S1", name: "Test-d2-s1-h1", other: "value", hierarchy: 1 },
    { department: "D2", section: "S2", name: "Test-d2-s2-h2", other: "value", hierarchy: 2 },
    { department: "D1", section: "S1", name: "Test-d1-s1-h1", other: "value", hierarchy: 1 },
    { department: "D2", section: "S2", name: "Test-d2-s2-h1", other: "value", hierarchy: 1 },
  ],

  result = input
    .sort((a, b) =>
      a.department.localeCompare(b.department)
      || a.section.localeCompare(b.section)
      || a.hierarchy - b.hierarchy)
    .reduce((a, { department, section, hierarchy, ...data }) => (
      ((a[department] ??= {})[section] ??= []).push(data), a
    ), {});

console.log(result)
代码语言:javascript
复制
.as-console-wrapper { max-height: 100% !important; top: 0; }

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68396148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档