JS日期数组如何按天分组 [英] Array Of JS Dates How To Group By Days

查看:158
本文介绍了JS日期数组如何按天分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出最优的并且用最少量的循环方式来对此js日期对象的数组进行分组:(注意这是浏览器控制台输出它的实际JS日期如新日期( ))

I'm trying to figure out the most optimal and with as minimum amount of loops way to group my array of js dates objects from this: (Take a note this is browser console output it's actully real JS dates like new Date())

[Sat Aug 08 2015 08:30:00 GMT+0200 (Central Europe Daylight Time), Sat Aug 08 2015 09:30:00 GMT+0200 (Central Europe Daylight Time), Sun Aug 09 2015 08:30:00 GMT+0200 (Central Europe Daylight Time), Sun Aug 09 2015 09:30:00 GMT+0200 (Central Europe Daylight Time), Mon Aug 10 2015 18:00:00 GMT+0200 (Central Europe Daylight Time), Mon Aug 10 2015 23:00:00 GMT+0200 (Central Europe Daylight Time), Tue Aug 11 2015 18:00:00 GMT+0200 (Central Europe Daylight Time), Tue Aug 11 2015 23:00:00 GMT+0200 (Central Europe Daylight Time), Wed Aug 12 2015 18:00:00 GMT+0200 (Central Europe Daylight Time), Wed Aug 12 2015 23:00:00 GMT+0200 (Central Europe Daylight Time)]

与oragnized数组同一天的每个日期然后我可以在UI8月08日显示它,并在当天显示2或多少日期。

to oragnized array with each date of the same day inside a "chunk" so I can display it on the UI "Aug 08" and show 2 or how many dates inside that day.

例如:

[{day: 'Aug 08', times:[Sat Aug 08 2015 08:30:00 GMT+0200 (Central Europe Daylight Time), Sat Aug 08 2015 09:30:00 GMT+0200 (Central Europe Daylight Time)]}]

我目前认为这样做的方式是

My current way I thought about doing it was

var startDays = _.map(occurences, function (date) {
  return moment(date).startOf('day').format();
});

之后获得独特的日子:

_.uniq(startDays, true)

在我之后得到了独特的日子,另一个循环将同一天添加到这个组,你可以看到,你可能会看到为什么我不喜欢它,这就是为什么我希望得到一些聪明的帮助,因为没有任何问题在我的头上。谢谢。

and after I got the unique days another loop to add the same day to this group as you can see by now you might see why I don't like it and this is why I would love to get some smart help because nothing gets to my head with this. Thank you.

推荐答案

Underscore有 _。groupBy 功能应该完全符合您的要求:

Underscore has the _.groupBy function which should do exactly what you want:

var groups = _.groupBy(occurences, function (date) {
  return moment(date).startOf('day').format();
});

这将返回一个对象,其中每个键都是一天,值是一个包含所有出现次数的数组那天。

This will return an object where each key is a day and the value an array containing all the occurrences for that day.

要将对象转换为与问题中相同形式的数组,您可以使用map:

To transform the object into an array of the same form as in the question you could use map:

var result = _.map(groups, function(group, day){
    return {
        day: day,
        times: group
    }
});

要进行分组,映射和排序,您可以执行以下操作:

To group, map and sort you could do something like:

var occurrenceDay = function(occurrence){
    return moment(occurrence).startOf('day').format();
};

var groupToDay = function(group, day){
    return {
        day: day,
        times: group
    }
};

var result = _.chain(occurences)
    .groupBy(occurrenceDay)
    .map(groupToDay)
    .sortBy('day')
    .value();

这篇关于JS日期数组如何按天分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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