【发布时间】:2018-11-23 21:07:27
【问题描述】:
我想根据当前月份和年份显示对象
这会根据月份过滤对象,我还需要根据年份过滤对象。
var array = [{
title: "a",
date: "2018-03-29"
}, {
title: "b",
date: "2018-04-13"
}, {
title: "c",
date: "2018-04-12"
}, {
title: "leave",
date: "2018-04-11"
}, {
title: "d",
date: "2018-06-16"
}],
currentMonth = new Date().getMonth() + 1,
events = array.filter(e => {
var [_, month] = e.date.split('-'); // Or, var month = e.date.split('-')[1];
return currentMonth === +month;
});
console.log(events);
【问题讨论】:
-
使用
year而不是_并对照当年检查? -
@CertainPerformance 谢谢我会
-
如
var [year, month] = e.date.split('-'); return currentYear === +year && currentMonth === +month;
标签: javascript arrays typescript object