使用 lodash 将 Typescript 中的项目分组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36955906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Group items in Typescript with lodash
提问by jlp
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"}
];
so it becomes:
所以它变成:
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"}
]
}
]
I was able to group them with _.groupBy and _.map
我能够用 _.groupBy 和 _.map 将它们分组
var g = _.groupBy(meetings, function(i){return getDatePart(i.date);});
var groupedMeetings = _.map(g, function(items,key){return {day:key, meetings:items};});
But I have problems with doing the same in Typescript with https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/lodash/lodash.d.ts
但是我在使用https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/lodash/lodash.d.ts在 Typescript 中做同样的事情时遇到了问题
First, I declared my classes:
首先,我声明了我的课程:
class Meeting
{
date: Date,
place: Meeting[]
}
class GroupedMeeting
{
constructor(private day: Date, private meetings: Meeting[]) {}
}
Now I dont know which overload of groupBy to use? And how to map it to list of grouped items?
现在我不知道要使用 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); });
回答by ryeballar
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()
.
您可以使用该_.groupBy()
方法并添加一个回调,该回调返回您需要将其分组的值,即日期的年、月、日部分。然后,您可以使用 设置所需的值_.map()
。
The example below uses ES6:
下面的例子使用 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>
Here is the ES5 version:
这是 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>
回答by arg20
If you have the typings for lodash (for newer versions of typescript do npm i --save-dev @types/lodash
), methods like _.groupBy or _.keyBy accept a generics type.
如果您有 lodash 的类型(对于较新版本的npm i --save-dev @types/lodash
typescript do ),像 _.groupBy 或 _.keyBy这样的方法接受泛型类型。
Eg: _.groupBy<TypeEachGroupCarries>(expression)
will return an array of arrays that contain items of the type TypeEachGroupCarries
.
例如:_.groupBy<TypeEachGroupCarries>(expression)
将返回一个数组数组,其中包含类型为 的项目TypeEachGroupCarries
。
回答by Bruno Grieder
Why use a library when you already have all the tools ? :)
当您已经拥有所有工具时,为什么还要使用库?:)
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]
}
})
See it running in the Typescript playground
看到它在 Typescript操场上运行