使用lodash在Typescript中对项目进行分组

时间:2016-04-30 14:10:29

标签: typescript lodash definitelytyped

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

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

对它们进行分组
var g = _.groupBy(meetings, function(i){return getDatePart(i.date);});
var groupedMeetings  = _.map(g, function(items,key){return {day:key, meetings:items};});

但我在使用https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/lodash/lodash.d.ts

的Typescript中做同样的事情时遇到了问题

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

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

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

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); });

3 个答案:

答案 0 :(得分:3)

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

以下示例使用ES6:

&#13;
&#13;
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>');
&#13;
<script src="https://cdn.jsdelivr.net/lodash/4.11.2/lodash.min.js"></script>
&#13;
&#13;
&#13;

以下是ES5版本:

&#13;
&#13;
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>');
&#13;
<script src="https://cdn.jsdelivr.net/lodash/4.11.2/lodash.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

如果你有lodash的类型(对于新版本的typescript do npm i --save-dev @types/lodash),_.groupBy或_.keyBy等方法接受泛型类型。

例如:_.groupBy<TypeEachGroupCarries>(expression)将返回包含TypeEachGroupCarries类型项的数组数组。

答案 2 :(得分:1)

为什么在已经拥有所有工具时使用库? :)

const dateMap  = meetings
    .reduce(
    (m, v) => {
        const day = v.date.substring(0,10)
        const entry = m[day]
        if (typeof entry === 'undefined') {
            m[day] = [v] 
        }
        else {
            entry.push(v)
        }
        return m
    },
    {}    
)

const groupedMeetings = Object.keys(dateMap).map(d => {
    return {
        day: d,
        meetings: dateMap[d]
    }
})    

在Typescript here

中查看它