问题:
我有一个对象数组,我们每月可以像这样获得员工的工作时间:
{
"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”,以便对于存在数据的月份,将其分配给month,否则我们针对月份添加空值。请看图片以了解清晰度:
使该月的工作时间不为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
};
}
});
});
这没有给我所需的结果,我们将不胜感激。谢谢!
答案 0 :(得分:1)
因此,给定条件,每个月条目在值对象中是不同的,最好循环抛出您的months数组。 尝试这个:
const YOUR_OBJECT = {
"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
};
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' }
];
const test = {};
scrollableTableAttribute.forEach( (month) => {
test[month.key] = YOUR_OBJECT.allocationHours.find((item) => month.key === item.monthYear) || {
workHours: 0, allocationId: YOUR_OBJECT.allocationId,
monthYear: month.key, lockStatus: 'N', comment: '', allocationHoursId: null,
year: null
};
});
console.log(test)
如果这对您有用,那么您可以根据自己的需要做得更好。
答案 1 :(得分:1)
我们可以使用map
方法来预测收到的所有月份到月份:
const months = new Map(json.allocationHours.map(s => [s.monthYear, s]));
let fallbackObject = {
"allocationHoursId": null,
"allocationId": null,
"monthYear": null,
"year": 2020,
"workHours": 40,
"lockStatus": "N",
"comments": null
}
json.allocationHours = scrollableTableAttribute.map(({key}) =>
({...months.get(key) || fallbackObject}));
一个例子:
let json = {
"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
}
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' }
];
const months = new Map(json.allocationHours.map(s => [s.monthYear, s]));
let fallbackObject = {
"allocationHoursId": null,
"allocationId": null,
"monthYear": null,
"year": 2020,
"workHours": 40,
"lockStatus": "N",
"comments": null
}
json.allocationHours = scrollableTableAttribute.map(({key}) => ({...months.get(key) || fallbackObject}));
console.log(json);
答案 2 :(得分:1)
由于您的item
具有开始日期和结束日期,因此您实际上可以生成跨越该时间段的所有月份,并考虑到这可能跨越一个或多个年限,并且可能在该范围内的任何地方开始/结束一年。
您可以将年/月组合转换为一个数字,代表一个绝对的月数。
这是看起来如何:
const item = {"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};
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const toMonths = (dateStr) => dateStr.slice(0, 4) * 12 + +dateStr.slice(5, 7) - 1;
const startMonth = toMonths(item.allocStartDate);
const result = Array.from({length: toMonths(item.allocEndDate) + 1 - startMonth}, (_, i) => ({
allocationHoursId: null,
allocationId: item.allocationId,
monthYear: months[(startMonth + i) % 12],
year: Math.floor((startMonth + i) / 12),
workHours: 0, lockStatus: "N", comments: ""
}));
for (const obj of item.allocationHours) {
result[obj.year * 12 + months.indexOf(obj.monthYear) - startMonth] = obj;
}
console.log(result);