【发布时间】:2020-06-22 09:50:00
【问题描述】:
问题:
我有一个对象数组,我们可以在其中获取员工每月的工作时间,如下所示:
{
"allocationId": 227,
"role": "Software Engineer",
"name": "Test User",
"country": "USA",
"state": "TX",
"city": "",
"allocStartDate": "2019-03-09",
"allocEndDate": "2020-03-10",
"allocationHours": [{
"allocationHoursId": 2250,
"allocationId": 227,
"monthYear": "February",
"year": 2020,
"workHours": 60,
"lockStatus": "Y",
"comments": null
},
{
"allocationHoursId": 2251,
"allocationId": 227,
"monthYear": "January",
"year": 2020,
"workHours": 10,
"lockStatus": "N",
"comments": null
},
{
"allocationHoursId": 2254,
"allocationId": 227,
"monthYear": "April",
"year": 2020,
"workHours": 40,
"lockStatus": "N",
"comments": null
}
],
"totalHours": 170
}
我需要针对一年中的月份格式化“allocationHours”,这样,对于存在数据的月份,它被分配给月份,否则我们在月份中添加空值。请看清楚图片:
使当月的工作时间不显示为 0。
我尝试过的:
const scrollableTableAttribute = [
{ key: 'January', value: '01' },
{ key: 'February', value: '02' },
{ key: 'March', value: '03' },
{ key: 'April', value: '04' },
{ key: 'May', value: '05' },
{ key: 'June', value: '06' },
{ key: 'July', value: '07' },
{ key: 'August', value: '08' },
{ key: 'September', value: '09' },
{ key: 'October', value: '10' },
{ key: 'November', value: '11' },
{ key: 'December', value: '12' }
];
And in the method -
item.allocationHours.forEach((element) => {
scrollableTableAttribute.map((month) => {
if (element.monthYear === month.key) {
test[month.key] = element;
} else {
test[month.key] = {
workHours: 0, allocationId: item.allocationId,
// tslint:disable-next-line: no-null-keyword
monthYear: month.key, lockStatus: 'N', comment: '', allocationHoursId: null, year: null
};
}
});
});
这没有给我所需的结果,任何帮助将不胜感激。谢谢!
【问题讨论】:
-
你能显示想要的结果吗?
-
@StepUp - 想要的结果是图像。
标签: javascript arrays json angular typescript