Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 针对另一个对象数组遍历对象数组_Javascript_Arrays_Json_Angular_Typescript - Fatal编程技术网

Javascript 针对另一个对象数组遍历对象数组

Javascript 针对另一个对象数组遍历对象数组,javascript,arrays,json,angular,typescript,Javascript,Arrays,Json,Angular,Typescript,问题: 我有一个对象数组,在其中我们可以得到员工每月的工作时间,如下所示: { "allocationId": 227, "role": "Software Engineer", "name": "Test User", "country": "USA", "state": "TX", "city": "", "allocStartDate": "2019-03-09", "allocEndDate": "2020-03-10",

问题:

我有一个对象数组,在其中我们可以得到员工每月的工作时间,如下所示:

{
    "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
            };
          }
        });
      });

这不是给我所需的结果,任何帮助将不胜感激。谢谢

因此,假设每个月条目在值的对象中是不同的,最好循环抛出months数组。 试试这个:
const您的\u对象={
“分配ID”:227,
“角色”:“软件工程师”,
“名称”:“测试用户”,
“国家”:“美国”,
“州”:“TX”,
“城市”:“,
“allocStartDate”:“2019-03-09”,
“AllocentDate”:“2020-03-10”,
“分配小时数”:[{
“AllocationHourId”:2250,
“分配ID”:227,
“月”:“二月”,
“年份”:2020年,
“工作时间”:60小时,
“锁定状态”:“Y”,
“注释”:空
},
{
“AllocationHourId”:2251,
“分配ID”:227,
“一月”:“一月”,
“年份”:2020年,
“工作时间”:10,
“锁定状态”:“N”,
“注释”:空
},
{
“AllocationHourId”:2254,
“分配ID”:227,
“月”:“四月”,
“年份”:2020年,
“工作时间”:40小时,
“锁定状态”:“N”,
“注释”:空
}
],
“总小时数”:170
};
常量scrollableTableAttribute=[
{键:'一月',值:'01'},
{键:'二月',值:'02'},
{键:'March',值:'03'},
{键:'April',值:'04'},
{键:'May',值:'05'},
{键:'June',值:'06'},
{键:'July',值:'07'},
{键:'August',值:'08'},
{键:“九月”,值:“09'},
{键:'10',值:'10'},
{键:'11',值:'11'},
{键:'December',值:'12'}
];
常量测试={};
scrollableTableAttribute.forEach((月)=>{
test[month.key]=您的_对象.allocationHours.find((item)=>month.key==item.monthYear)|{
工作时间:0,分配ID:YOUR_OBJECT.allocationId,
monthYear:month.key,lockStatus:'N',注释:'',AllocationHourId:null,
年份:空
};
});

console.log(test)
我们可以使用
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={
“分配ID”:227,
“角色”:“软件工程师”,
“名称”:“测试用户”,
“国家”:“美国”,
“州”:“TX”,
“城市”:“,
“allocStartDate”:“2019-03-09”,
“AllocentDate”:“2020-03-10”,
“分配小时数”:[{
“AllocationHourId”:2250,
“分配ID”:227,
“月”:“二月”,
“年份”:2020年,
“工作时间”:60小时,
“锁定状态”:“Y”,
“注释”:空
},
{
“AllocationHourId”:2251,
“分配ID”:227,
“一月”:“一月”,
“年份”:2020年,
“工作时间”:10,
“锁定状态”:“N”,
“注释”:空
},
{
“AllocationHourId”:2254,
“分配ID”:227,
“月”:“四月”,
“年份”:2020年,
“工作时间”:40小时,
“锁定状态”:“N”,
“注释”:空
}
],
“总小时数”:170
}
常量scrollableTableAttribute=[
{键:'一月',值:'01'},
{键:'二月',值:'02'},
{键:'March',值:'03'},
{键:'April',值:'04'},
{键:'May',值:'05'},
{键:'June',值:'06'},
{键:'July',值:'07'},
{键:'August',值:'08'},
{键:“九月”,值:“09'},
{键:'10',值:'10'},
{键:'11',值:'11'},
{键:'December',值:'12'}
];
const months=newmap(json.allocationHours.Map(s=>[s.monthYear,s]);
让fallbackObject={
“AllocationHourId”:空,
“allocationId”:空,
“monthYear”:空,
“年份”:2020年,
“工作时间”:40小时,
“锁定状态”:“N”,
“注释”:空
}
json.allocationHours=scrollableTableAttribute.map(({key})=>({…months.get(key)| | fallbackObject}));

log(json)由于您的
项目有开始日期和结束日期,您实际上可以生成跨越该期间的所有月份,考虑到这可能跨越一年或多年的边界,并且可能在一年内的任何地方开始/结束

您可以将年/月组合转换为一个数字,表示月份的绝对数

下面是它的样子:

const item={“allocationId”:227,“角色”:“软件工程师”,“姓名”:“测试用户”,“国家”:“美国”,“州”:“TX”,“城市”:“allocStartDate”:“2019-03-09”,“AllocentDate”:“2020-03-10”,“AllocationHourId”:2250,“allocationId”:227,“月”:“2月”,“年”:2020,“工作时间”:60,“锁定状态”:“Y”,“注释”:null}{“allocationHoursId”:2251,“allocationId”:227,“monthYear”:“一月”,“年”:2020,“工作时间”:10,“锁定状态”:“N”,“注释”:null},{“allocationHoursId”:2254,“allocationId”:227,“monthYear”:“四月”,“年”:2020,“工作时间”:40,“锁定状态”:“N”,“注释”:null}],“总小时数”:170};
常量月=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”];
常量toMonths=(dateStr)=>dateStr.slice(0,4)*12++dateStr.slice(5,7)-1;
const startMonth=明天(项目allocStartDate);
const result=Array.from({length:toMonths(item.allocEndDate)+1-startMonth},({u,i)=>({
AllocationHourId:null,
分配ID