如何使用lodash groupBy,sumBy和orderBy并保持数组的结构

时间:2017-08-11 04:15:49

标签: javascript lodash

我有以下对象数组,我希望按应用名称进行分组,然后按时间对该应用进行求和,然后按顺序排列,以便在不更改数组结构的情况下为我提供最常用的应用

[ 
{ appName: GoogleChrome, Position: GoogleChrome, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51", Totaltime:1},
{ appName: GoogleChrome, Position: GoogleChrome, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51",Totaltime:1},
{ appName: GoogleChrome, Position: GoogleChrome, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51", Totaltime:1},
{ appName: Code, Position: Code, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51",Totaltime:1},
{ appName: Code, Position: Code, start: "2017/04/07 05:00:51" end:"2017/04/07 05:15:51",Totaltime:15},
{ appName: Slack, Position: Slack, start: "2017/04/07 05:10:51" end:"2017/04/07 05:20:51",Totaltime:10},
{ appName: Slack, Position: Slack, start: "2017/04/07 05:30:00" end:"2017/04/07 05:50:51",Totaltime:20},
{ appName: Slack, Position: Slack, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51",Totaltime:1},
{ appName: Slack, Position: Slack, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51",Totaltime:1} ]

我希望得到以下结果

[ 
{ appName: Slack, Position: Slack, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51",Totaltime:32},
{ appName: Slack, Position: Slack, start: "2017/04/07 05:30:00" end:"2017/04/07 05:50:51",Totaltime:32},
{ appName: Slack, Position: Slack, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51",Totaltime:32},
{ appName: Slack, Position: Slack, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51",Totaltime:32},
{ appName: Code, Position: Code, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51",Totaltime:16},
{ appName: Code, Position: Code, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51",Totaltime:16},
{ appName: GoogleChrome, Position: GoogleChrome, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51", Totaltime:3},
{ appName: GoogleChrome, Position: GoogleChrome, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51",Totaltime:3},
{ appName: GoogleChrome, Position: GoogleChrome, start: "2017/04/07 05:31:51" end:"2017/04/07 05:31:51", Totaltime:3} ]

一旦我得到结果,我将从总时间元素中做_dropright。 请理解以上是虚拟数据。没有实际值。

1 个答案:

答案 0 :(得分:1)

我注意到您的第二组示例数据已修改Totaltime。我不确定这是不是故意或有意,因为你说你不想修改原始数组,但我的解决方案假设所有值(包括Totaltime)都保持与我们开始时完全相同的方式。此外,虚拟数据需要稍微修改才能按原样工作,因此我添加了一些小的调整(逗号,字符串等)以使其运行。

最后,我在原始列表中移动了项目(不修改任何值)以显示条目确实已排序。

以下代码采用以下列表:

  • 按appName分组
  • 使用给定appName按照最长时间的降序排序多个条目
  • 按照其总计时间降序对分组的appName条目进行排序
  • 将所有内容拼合成一个数组

如果您不希望/需要在appName中排序的单个条目,您只需删除.map()电话。

const data = [
{ appName: 'GoogleChrome', Position: 'GoogleChrome', start: "2017/04/07 05:31:51", end:"2017/04/07 05:31:51", Totaltime:1},
{ appName: 'Slack', Position: 'Slack', start: "2017/04/07 05:31:51", end:"2017/04/07 05:31:51",Totaltime:1},
{ appName: 'GoogleChrome', Position: 'GoogleChrome', start: "2017/04/07 05:31:51", end:"2017/04/07 05:31:51", Totaltime:1},
{ appName: 'Code', Position: 'Code', start: "2017/04/07 05:31:51", end:"2017/04/07 05:31:51",Totaltime:1},
{ appName: 'Slack', Position: 'Slack', start: "2017/04/07 05:30:00", end:"2017/04/07 05:50:51",Totaltime:20},
{ appName: 'Code', Position: 'Code', start: "2017/04/07 05:00:51", end:"2017/04/07 05:15:51",Totaltime:15},
{ appName: 'GoogleChrome', Position: 'GoogleChrome', start: "2017/04/07 05:31:51", end:"2017/04/07 05:31:51",Totaltime:1},
{ appName: 'Slack', Position: 'Slack', start: "2017/04/07 05:10:51", end:"2017/04/07 05:20:51",Totaltime:10},
{ appName: 'Slack', Position: 'Slack', start: "2017/04/07 05:31:51", end:"2017/04/07 05:31:51",Totaltime:1} ];

function reorder(list) {
    const newList = _(list)
        .groupBy('appName')
        .map(element => {
            return _.sortBy(element, innerElement => {
                return -innerElement.Totaltime;
            });
        })
        .sortBy(element => {
            return -_.sumBy(element, 'Totaltime');
        })
        .flatten()
        .valueOf();


    console.log(newList);
    return newList;
}

reorder(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

结果:

[ { appName: 'Slack', Position: 'Slack', start: '2017/04/07 05:30:00', end: '2017/04/07 05:50:51', Totaltime: 20 },
{ appName: 'Slack', Position: 'Slack', start: '2017/04/07 05:10:51', end: '2017/04/07 05:20:51', Totaltime: 10 },
{ appName: 'Slack', Position: 'Slack', start: '2017/04/07 05:31:51', end: '2017/04/07 05:31:51', Totaltime: 1 },
{ appName: 'Slack', Position: 'Slack', start: '2017/04/07 05:31:51', end: '2017/04/07 05:31:51', Totaltime: 1 },
{ appName: 'Code', Position: 'Code', start: '2017/04/07 05:00:51', end: '2017/04/07 05:15:51', Totaltime: 15 },
{ appName: 'Code', Position: 'Code', start: '2017/04/07 05:31:51', end: '2017/04/07 05:31:51', Totaltime: 1 },
{ appName: 'GoogleChrome', Position: 'GoogleChrome', start: '2017/04/07 05:31:51', end: '2017/04/07 05:31:51', Totaltime: 1 },
{ appName: 'GoogleChrome', Position: 'GoogleChrome', start: '2017/04/07 05:31:51', end: '2017/04/07 05:31:51', Totaltime: 1 },
{ appName: 'GoogleChrome', Position: 'GoogleChrome', start: '2017/04/07 05:31:51', end: '2017/04/07 05:31:51', Totaltime: 1 } ]