【问题标题】:Group and Sort by multiple fields in lodash按 lodash 中的多个字段分组和排序
【发布时间】:2021-09-24 11:59:23
【问题描述】:

假设这个数组:

[
    { 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 }
]

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

结果是:

[
  "D1": [
     "S1": [
       { name: "Test", other: "value" }
     ]
  ],
  "D2": [
     "S1" [
       { name: "Test", other: "value" }
     ],
     "S2" [
       { name: "Test", other: "value" },
       { name: "Test", other: "value" }
     ]
  ]
]

这是我前进方向的骨架:

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 实现它?

【问题讨论】:

  • 您的结果不是有效的 javascript。

标签: javascript arrays sorting lodash


【解决方案1】:

您可以在原版 javascript 中使用 sort()reduce() 来实现此目的。

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)
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

    【解决方案2】:

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

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

    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);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-19
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 2018-05-17
      • 1970-01-01
      相关资源
      最近更新 更多