我有以下对象数组,我希望按应用名称进行分组,然后按时间对该应用进行求和,然后按顺序排列,以便在不更改数组结构的情况下为我提供最常用的应用
[
{ 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
。
请理解以上是虚拟数据。没有实际值。
答案 0 :(得分:1)
我注意到您的第二组示例数据已修改Totaltime。我不确定这是不是故意或有意,因为你说你不想修改原始数组,但我的解决方案假设所有值(包括Totaltime)都保持与我们开始时完全相同的方式。此外,虚拟数据需要稍微修改才能按原样工作,因此我添加了一些小的调整(逗号,字符串等)以使其运行。
最后,我在原始列表中移动了项目(不修改任何值)以显示条目确实已排序。
以下代码采用以下列表:
如果您不希望/需要在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 } ]