在Javascript中按月份分组日期
在Javascript中,如何对日期数组进行分组,以获得具有按月份分组的日期的新对象数组 我有:在Javascript中按月份分组日期,javascript,Javascript,在Javascript中,如何对日期数组进行分组,以获得具有按月份分组的日期的新对象数组 我有: const arr = [ Date Sat Dec 31 2016 01:00:00 GMT+0100 (Central European Standard Time), Date Sun Jan 01 2017 01:00:00 GMT+0100 (Central European Standard Time), Date Mon Jan 02 2017 01:00:00 GMT+0100
const arr = [
Date Sat Dec 31 2016 01:00:00 GMT+0100 (Central European Standard Time),
Date Sun Jan 01 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Mon Jan 02 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Tue Jan 31 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Wed Feb 01 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Thu Feb 02 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Fri Feb 03 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Sat Feb 04 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Sun Feb 05 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Mon Feb 06 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Tue Feb 07 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Wed Feb 08 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Thu Feb 09 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Fri Feb 10 2017 01:00:00 GMT+0100 (Central European Standard Time),
]
我想得到:
const nArr = [
{
name: December,
dates:[
Date Sat Dec 31 2016 01:00:00 GMT+0100 (Central European Standard Time)
]
},
{
name: January,
dates:[
Date Sun Jan 01 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Mon Jan 02 2017 01:00:00 GMT+0100 (Central European Standard Time),
Date Tue Jan 31 2017 01:00:00 GMT+0100 (Central European Standard Time),
]
}
....
]
我尝试使用数组减缩器,但我有问题
arr.reduce((acc, val) => {
const d = new Date(val)
let m = months[d.getMonth()]
acc[m] = acc[m]
acc[m].push(val)
return acc
}, [])
谢谢你的帮助,我是JS新手,我不是专家,但这就是我解决问题的方法
const arr = [
new Date('Date Sat Dec 31 2016 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Sun Jan 01 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Mon Jan 02 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Tue Jan 31 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Wed Feb 01 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Thu Feb 02 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Fri Feb 03 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Sat Feb 04 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Sun Feb 05 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Mon Feb 06 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Tue Feb 07 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Wed Feb 08 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Thu Feb 09 2017 01:00:00 GMT+0100 (Central European Standard Time'),
new Date('Date Fri Feb 10 2017 01:00:00 GMT+0100 (Central European Standard Time'),
];
let months = [];
for (var i = 0; i < arr.length; i++) {
var month = arr[i].getMonth();
if (!months[month]) {
months[month] = [];
}
months[month].push(arr[i]);
}
console.log(months);
const arr=[
新日期(日期:2016年12月31日星期六01:00:00 GMT+0100(中欧标准时间)),
新日期(“日期太阳2017年1月1日01:00:00 GMT+0100(中欧标准时间”),
新日期(“日期2017年1月2日周一01:00:00 GMT+0100(中欧标准时间”),
新日期(“日期2017年1月31日星期二01:00:00 GMT+0100(中欧标准时间”),
新日期(“日期2017年2月1日星期三01:00:00 GMT+0100(中欧标准时间”),
新日期(“日期2017年2月2日星期四01:00:00 GMT+0100(中欧标准时间”),
新日期(“日期2017年2月3日星期五01:00:00 GMT+0100(中欧标准时间”),
新日期(日期:2017年2月4日星期六01:00:00 GMT+0100(中欧标准时间)),
新日期(“日期太阳2017年2月5日01:00:00 GMT+0100(中欧标准时间”),
新日期(日期:2017年2月6日星期一01:00:00 GMT+0100(中欧标准时间)),
新日期(“日期2017年2月7日星期二01:00:00 GMT+0100(中欧标准时间”),
新日期(“日期2017年2月8日星期三01:00:00 GMT+0100(中欧标准时间”),
新日期(“日期2017年2月9日星期四01:00:00 GMT+0100(中欧标准时间”),
新日期(“日期2017年2月10日星期五01:00:00 GMT+0100(中欧标准时间”),
];
让月份=[];
对于(变量i=0;i
您可以通过将区域设置字符串格式化为month:long
来获取完整的月份“name”
const tolong month=(日期)=>
日期。Tolocalesting('en-US',{month:'long'});
const groupBy=(arr,fn)=>arr.reduce((acc,item)=>
(月份=>({
…acc,
[月份]:[…(acc[月份]| |[]),项目]
}))
(fn(项目)),{};
const groupByMonth=(日期)=>
对象.条目(分组依据(日期、月份))
.map(([name,dates])=>({name,dates}))
常数日期=[
新日期(“2016年12月31日星期六01:00:00 GMT+0100(中欧标准时间)”,
新日期(“2017年1月1日星期日01:00:00 GMT+0100(中欧标准时间)”,
新日期(“2017年1月2日星期一01:00:00 GMT+0100(中欧标准时间)”,
新日期(2017年1月31日星期二01:00:00 GMT+0100(中欧标准时间)),
新日期(“2017年2月1日星期三01:00:00 GMT+0100(中欧标准时间)”,
新日期(2017年2月2日星期四01:00:00 GMT+0100(中欧标准时间)),
新日期(“2017年2月3日星期五01:00:00 GMT+0100(中欧标准时间)”,
新日期(“2017年2月4日星期六01:00:00 GMT+0100(中欧标准时间)”,
新日期(2017年2月5日星期日01:00:00 GMT+0100(中欧标准时间)),
新日期(“2017年2月6日星期一01:00:00 GMT+0100(中欧标准时间)”,
新日期(2017年2月7日星期二01:00:00 GMT+0100(中欧标准时间)),
新日期(2017年2月8日星期三01:00:00 GMT+0100(中欧标准时间)),
新日期(2017年2月9日星期四01:00:00 GMT+0100(中欧标准时间)),
新日期(2017年2月10日星期五01:00:00 GMT+0100(中欧标准时间))
];
console.log(groupByMonth(dates));
.as控制台包装{top:0;最大高度:100%!important;}
const dates=[new Date(),new Date()];
常数月=[
“一月”,
“二月”,
“三月”,
“四月”,
“五月”,
“六月”,
“七月”,
“八月”,
“9月”,
“十月”,
“11月”,
“12月”
];
常量sortedDates:Array=[];
dates.map(日期=>{
const month=月[date.getMonth()];
const monthObj=sortedDates.find(datesByMonth=>datesByMonth.name==month)
if(monthObj==未定义){
推({
姓名:年月日,
日期:[
日期
]
})
回来
}
monthObj.dates.push(日期);
});
Array reduce是我尝试的第一步。reduce((acc,val)=>{const d=new Date(val)让m=months[d.getMonth()]acc[m]=acc[m]acc[m]。push(val)return acc},[])非常感谢
const dates = [new Date(), new Date()];
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
const sortedDates: Array<{name: string, dates: Array<Date>}> = [];
dates.map(date => {
const month = months[date.getMonth()];
const monthObj = sortedDates.find(datesByMonth => datesByMonth.name === month)
if (monthObj === undefined) {
sortedDates.push({
name: month,
dates: [
date
]
})
return;
}
monthObj.dates.push(date);
});