使用 lodash 将 Typescript 中的项目分组 [英] Group items in Typescript with lodash

查看:53
本文介绍了使用 lodash 将 Typescript 中的项目分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按日期对一组对象进行分组:

I need to group a collection of objects by date:

var meetings =
[
   {date:"2001-01-01 13:00", place:"park"},
   {date:"2001-01-01 14:00", place:"school"},
   {date:"2001-01-02 11:00", place:"house"}
];

因此它变为:

var groupedMeetings = 
[
   {
      day:"2001-01-01", 
      meetings: 
      [
         {date:"2001-01-01 13:00", place:"park"},
         {date:"2001-01-01 14:00", place:"school"}
      ]
   },
   {
      day:"2001-01-02", 
      meetings: 
      [
         {date:"2001-01-02 11:00", place:"house"}
      ]
   }
]

我能够将它们与_.groupBy和_.map分组

I was able to group them with _.groupBy and _.map

var g = _.groupBy(meetings, function(i){return getDatePart(i.date);});
var groupedMeetings  = _.map(g, function(items,key){return {day:key, meetings:items};});

但是我在使用 https的Typescript中执行相同操作时遇到问题://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/lodash/lodash.d.ts

首先,我宣布了自己的课程:

First, I declared my classes:

class Meeting
    {
       date: Date,
       place: Meeting[]
    }
class GroupedMeeting
    {
      constructor(private day: Date, private meetings: Meeting[]) {}
    }

现在我不知道要使用groupBy的哪个重载?以及如何将其映射到分组项目列表?

Now I dont know which overload of groupBy to use? And how to map it to list of grouped items?

var grouped: _.Dictionary<Meeting[]> = _.groupBy(meetings, (i: Meeting) => { return this.getDatePart(i.date); });

var groupedMeetings : GroupedMeeting[] = _.map(grouped, (items: Meeting[], key: Date) => { return new GroupedMeeting (key, items); });

推荐答案

您可以使用 _.groupBy() 方法并添加一个回调,该回调返回您需要将其分组的值,该值是日期的年,月,日部分.然后,您可以使用 _.map() 来设置所需的值.

You can make use of the _.groupBy() method and add a callback that returns the value you need to group it with, which is the year, month, day part of the date. You can then set the values needed by using _.map().

下面的示例使用ES6:

The example below uses ES6:

var meetings =
[
   {date:"2001-01-01 13:00", place:"park"},
   {date:"2001-01-01 14:00", place:"school"},
   {date:"2001-01-02 11:00", place:"house"}
];

var result = _(meetings)
  .groupBy(meeting => meeting.date.split(' ').shift())
  .map((meetings, day) => ({ day, meetings }))
  .value();

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

<script src="https://cdn.jsdelivr.net/lodash/4.11.2/lodash.min.js"></script>

这是ES5版本:

var meetings =
[
   {date:"2001-01-01 13:00", place:"park"},
   {date:"2001-01-01 14:00", place:"school"},
   {date:"2001-01-02 11:00", place:"house"}
];

var result = _(meetings)
  .groupBy(function(meeting) { return meeting.date.split(' ').shift(); })
  .map(function(meetings, day) { return { day: day, meetings: meetings }; })
  .value();

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

<script src="https://cdn.jsdelivr.net/lodash/4.11.2/lodash.min.js"></script>

这篇关于使用 lodash 将 Typescript 中的项目分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆