Data Transform
Data Transform
by Rishabh Sharma
JavaScript
function parse() {
var data = getData(),
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
years = [2017, 2018, 2019, 2020],
schedule = {};
years.forEach(function(year) {
schedule[year] = [];
months.forEach(function(month, monthIndex) {
schedule[year].push({
month: month,
tableList: []
});
data.forEach(function(item) {
if (item.dataUploadScheduleType === 'Monthly') {
let date = new Date();
let dayNum = date.getDate();
if (dayNum >= item.monthlyDataUploadSchedule.startDay && dayNum <= item.monthlyDataUploadSchedule.endDay) {
schedule[year][monthIndex].tableList.push({
name: item.tableName
});
}
} else if (item.dataUploadScheduleType === 'OneTimeRequest') {
let date = new Date();
let timestamp = date.getTime();
if (timestamp >= item.oneTimeDataUploadSchedule.startDate && timestamp <= item.oneTimeDataUploadSchedule.endDate) {
schedule[year][monthIndex].tableList.push({
name: item.tableName
});
}
}
});
});
});
console.log(schedule);
}
parse();
function getData() {
var data = [{
"id": 35,
"tableName": "hello",
"dataUploadScheduleType": "Monthly",
"monthlyDataUploadSchedule": {
"id": 42,
"startDay": 1,
"endDay": 30
},
"oneTimeDataUploadSchedule": null
}, {
"id": 36,
"tableName": "dfghj",
"dataUploadScheduleType": "OneTimeRequest",
"monthlyDataUploadSchedule": null,
"oneTimeDataUploadSchedule": {
"id": 23,
"startDate": 1484159700000,
"endDate": 1485196500000
}
}];
return data;
}