仅汇总对象的数字属性

时间:2019-05-22 10:24:49

标签: javascript oop

我创建了一个方法,该方法返回多个mapdObjecst,其中包含一个ID和每天记录的小时数。

这是返回映射的代码:

 return Object.keys(tableData).map(key => {
      const mappedObject = {
        id: key
      };
      tableData[key].forEach(weekday => {
          mappedObject[weekday.day] = Math.round( (weekday.spent / 3600) * 10) / 10;
      });
      return mappedObject;
    });

这是返回的mappingObjects:

{id: "c94.33", MON: 8, WED: 3.2, FRI: 5.3}
{id: "c.005.001", THU: 8}
{id: "unspecified", TUE: 8, WED: 4.8, FRI: 2.7}

如何计算每个ID下的小时数?

例如:

c94.33:16.5

c.005.001:8

未指定:15.5

我的想法和尝试的内容: -创建一个数组,然后将其推入到该数组的appedObject值中。

    return Object.keys(tableData).map(key => {
      const mappedObject = {
        id: key
      };
      tableData[key].forEach(weekday => {
          mappedObject[weekday.day] = Math.round( (weekday.spent / 3600) * 10) / 10;
          this.totalHoursPerid.push(_.sum(_.values(mappedObject)));
          console.log(this.totalHoursPerid);
      });
      return mappedObject;
    });
  }

这不起作用,因为id键具有字符串值(至少多数民众赞成在我的想法)。

根据注释中的要求,这是要解析的tableData:

{c94.33:
 [{day: "MON", spent: 28800},
  {day: "WED", spent: 11400},
  {day: "FRI", spent: 19200}],
c.005.001: 
 [{day: "THU", spent: 28800}],
unspecified:
 [{day: "TUE", spent: 28800},
  {day: "WED", spent: 17400},
  {day: "FRI", spent: 9600}]
}

3 个答案:

答案 0 :(得分:2)

您可以使用reduce。提取id,然后可以应用Object.values来获取其他属性的值。

const input = [
    {id: "c94.33", MON: 8, WED: 3.2, FRI: 5.3}, 
    {id: "c.005.001", THU: 8}, 
    {id: "unspecified", TUE: 8, WED: 4.8, FRI: 2.7}
];

const output = input.reduce((accu, {id, ...rest}) => {
    accu[id] = Object.values(rest).reduce((accu, val) => accu + val, 0);
    return accu;
}, {});

console.log(output);

您也可以使用map

const input = [
    {id: "c94.33", MON: 8, WED: 3.2, FRI: 5.3}, 
    {id: "c.005.001", THU: 8}, 
    {id: "unspecified", TUE: 8, WED: 4.8, FRI: 2.7}
];

const output = input.map(({id, ...rest}) => ({[id]: Object.values(rest).reduce((accu, val) => accu + val, 0)}));
console.log(output);

const tableData = {
    "c94.33": [
        {day: "MON", spent: 28800},
        {day: "WED", spent: 11400},
        {day: "FRI", spent: 19200}
    ],
    "c.005.001": [
        {day: "THU", spent: 28800}
    ],
   "unspecified":[
       {day: "TUE", spent: 28800},
       {day: "WED", spent: 17400},
       {day: "FRI", spent: 9600}
    ]
}

const output = Object.entries(tableData).map(([key, val]) => {
    const sum = val.reduce((accu, {spent}) => {
        accu = accu + Math.round( (spent / 3600) * 10) / 10;
        return accu;
    }, 0)
    return ({[key]: sum})
});

console.log(output);

答案 1 :(得分:2)

let tableData = {"c94.33":
    [{day: "MON", spent: 28800},
     {day: "WED", spent: 11400},
     {day: "FRI", spent: 19200}],
   "c.005.001": 
    [{day: "THU", spent: 28800}],
   "unspecified":
    [{day: "TUE", spent: 28800},
     {day: "WED", spent: 17400},
     {day: "FRI", spent: 9600}]
   }


console.log( Object.keys(tableData).map(key => {
    const mappedObject = {};
      
    var spenttime = 0
    tableData[key].forEach(weekday => {
        spenttime  += Math.round( (weekday.spent / 3600) * 10) / 10;
    });

    mappedObject[key] = spenttime
    return mappedObject;
}));

答案 2 :(得分:1)

在创建新输出时,您可以将所有内容组合起来并计算出这些天的总和:

const tableData = {"c94.33":[{"day":"MON","spent":28800},{"day":"WED","spent":11400},{"day":"FRI","spent":19200}],"c.005.001":[{"day":"THU","spent":28800}],"unspecified":[{"day":"TUE","spent":28800},{"day":"WED","spent":17400},{"day":"FRI","spent":9600}]};

const calc = spent => Math.round((spent / 3600) * 10) / 10;

// Iterate over the object entries
const out = Object.entries(tableData).reduce((acc, [key, dayArr]) => {

  // `map` over the day array to calculate the spend
  const days = dayArr.map(({ day, spent }) => ({ [day]: calc(spent) }));

  // Use that array to calculate the sum
  const spent = days.reduce((acc, obj) => acc + Object.values(obj)[0], 0);

  // Create and return a new object
  acc[key] = { days, spent };
  return acc;
}, {});

console.log(out);