我有一个对象数组:
[{from:'2017-05-02',to:'2017-05-12',event:'google map launch day'},
{from:'2017-05-03',to:'2017-05-14',event:'marie"s farewell'},
{from:'2017-05-20',to:'2017-05-20',event:'iphone showcase'}]
我想将事件属性映射到
下面的对象数组[{
date: "2017-05-01"
},
{
date: "2017-05-02",
event: ['google map launch day']
},
{
date: "2017-05-03",
event: ['google map launch day', 'marie"s farewell']
},
{
date: "2017-05-04",
event: ['google map launch day', 'marie"s farewell']
}
..
]
https://pastebin.com/raw/Uv3U8zCy
我无法控制第一个对象数组,它来自外部API,我想构建一个自定义日历,我正在努力映射到一个新的对象数组格式。
function generateDates(startDate, stopDate) {
var dateArray = [];
var currentDate = moment(startDate);
var stopDate = moment(stopDate);
while (currentDate <= stopDate) {
dateArray.push({ date: moment(currentDate).format('YYYY-MM-DD') });
currentDate = moment(currentDate).add(1, 'days');
}
return dateArray;
}
generateDates('2017-05-01', '2017-05-31');
我可以使用momentjs生成一个月的日子,但我仍然卡住了。
答案 0 :(得分:0)
您可以使用哈希表作为生成日期的引用,然后以相同的样式迭代事件。
function getRanges(data, startDate, stopDate) {
function generateDates(startDate, stopDate) {
var currentDate = moment(startDate),
stopDate = moment(stopDate),
date;
while (currentDate <= stopDate) {
date = moment(currentDate).format('YYYY-MM-DD');
hash[date] = { date: date };
dateArray.push(hash[date]);
currentDate = moment(currentDate).add(1, 'days');
}
}
var dateArray = [],
hash = {};
generateDates(startDate, stopDate);
data.forEach(function (a) {
var currentDate = moment(a.from),
stopDate = moment(a.to),
date;
while (currentDate <= stopDate) {
date = moment(currentDate).format('YYYY-MM-DD');
hash[date].event = hash[date].event || [];
hash[date].event.push(a.event);
currentDate = moment(currentDate).add(1, 'days');
}
});
return dateArray;
}
var data = [{ from: '2017-05-02', to: '2017-05-12', event: 'google map launch day' }, { from: '2017-05-03', to: '2017-05-14', event: 'marie"s farewell' }, { from: '2017-05-20', to: '2017-05-20', event: 'iphone showcase' }];
console.log(getRanges(data, '2017-05-01', '2017-05-31'));
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
&#13;
答案 1 :(得分:0)
您可以使用.isBetween的moment.js并从迭代日期获取事件,如下所示。
var eventData = [{from:'2017-05-02',to:'2017-05-12',event:'google map launch day'},
{from:'2017-05-03',to:'2017-05-14',event:'marie"s farewell'},
{from:'2017-05-20',to:'2017-05-20',event:'iphone showcase'}]
// Get events for passed date
function getEvents(curDate){
return eventData.reduce(function(res,obj){
if(moment(curDate).isBetween(obj.from, obj.to, null, '[]'))
res.push(obj.event);
return res;
},[])
}
function generateDates(startDate, stopDate) {
var dateArray = [];
var dateArray = [];
var currentDate = moment(startDate);
var stopDate = moment(stopDate);
while (currentDate <= stopDate) {
dateArray.push({
date: moment(currentDate).format('YYYY-MM-DD'),
event:getEvents(currentDate) // Get events array here
});
currentDate = moment(currentDate).add(1, 'days');
}
return dateArray;
}