【问题标题】:Loop through array of objects against another array of objects针对另一个对象数组循环遍历对象数组
【发布时间】: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


【解决方案1】:

因此,在每个月条目在值对象中不同的条件下,最好循环抛出您的月份数组。 尝试这个:

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)

如果这对你有用,你可以根据你的需要做得更好。

【讨论】:

    【解决方案2】:

    我们可以使用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);

    【讨论】:

      【解决方案3】:

      由于您的 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);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-12
        • 1970-01-01
        • 2016-07-28
        • 2017-01-29
        相关资源
        最近更新 更多