如何在JavaScript中的对象数组中求和

时间:2019-06-18 11:24:22

标签: javascript

我有一个对象数组,像这样:

[
    {
        Daypart: "G_POSTPEAK",
        day_of_week: "Monday",
        uplift: 1
    },
    {
        Daypart: "A_BREAKFAST",
        day_of_week: "Thursday",
        uplift: 1
    },
    {
        Daypart: "C_DAYTIME",
        day_of_week: "Sunday",
        uplift: 2
    },
    {
        Daypart: "G_POSTPEAK",
        day_of_week: "Monday",
        uplift: 2
    },
]

我只显示了数组中对象的示例,我正在做更多工作。它们都具有指定的属性,daypart属性可以是8个值之一,而“星期几”值可以是7个(星期几)之一。

我想返回所有具有相同dayday和day_of_week值的对象的提升值的总和。

所以上面的代码应该返回类似的内容

{
    G_POSTPEAK_Monday: {
        Daypart: "G_POSTPEAK",
        day_of_week: "Monday",
        uplift: 3
    },
    A_BREAKFAST_Thursday: {
        Daypart: "A_BREAKFAST",
        day_of_week: "Thursday",
        uplift: 1
    },

    C_DAYTIME_Sunday: {
        Daypart: "C_DAYTIME",
        day_of_week: "Sunday",
        uplift: 2
    }
}

感谢任何帮助

5 个答案:

答案 0 :(得分:1)

可以使用以下功能。我用过ES6。该函数采用inputs作为输入对象。

const sumIt = (inputs) => {
  const result = {};
  inputs.forEach((input) => {
    const key = `${input.Daypart}_${input.day_of_week}`;
    if (key in result) {
      result[key].uplift = result[key].uplift + input.uplift;
    } else {
      result[key] = { ...input };
    }
  });
  return result;
};

答案 1 :(得分:0)

您可以使用ES6的reduce函数在一行中执行求和,而不是使用foreach。

let array = [
    {
        Daypart: "G_POSTPEAK",
        day_of_week: "Monday",
        uplift: 1
    },
    {
        Daypart: "A_BREAKFAST",
        day_of_week: "Thursday",
        uplift: 1
    },
    {
        Daypart: "C_DAYTIME",
        day_of_week: "Sunday",
        uplift: 2
    },
    {
        Daypart: "G_POSTPEAK",
        day_of_week: "Monday",
        uplift: 2
    },
];

const totalUplift = array.reduce((acc, array) => acc + array.uplift, 0);

console.log(totalUplift);

答案 2 :(得分:0)

您可以使用array#reduce在对象累加器中基于Daypartday_of_week对数据进行分组。

let data = [ { Daypart: "G_POSTPEAK", day_of_week: "Monday", uplift: 1 }, { Daypart: "A_BREAKFAST", day_of_week: "Thursday", uplift: 1 }, { Daypart: "C_DAYTIME", day_of_week: "Sunday", uplift: 2 }, { Daypart: "G_POSTPEAK", day_of_week: "Monday", uplift: 2 }],
    result = data.reduce((r,o) => {
      let key = `${o.Daypart}_${o.day_of_week}`;
      r[key] = r[key] || {...o, uplift: 0};
      r[key].uplift += o.uplift;
      return r;
    },{});
console.log(result);
.as-console-wrapper {max-height: 100% !important; top: 0;}

答案 3 :(得分:0)

一个功能更强的版本,像其他几种解决方案一样使用reduce

const combine = inputs => Object .values (
  inputs .reduce ((a, {Daypart, day_of_week, uplift}) => {
    const key = `${Daypart}|${day_of_week}`
    return {...a, [key]: ({
      Daypart, 
      day_of_week, 
      uplift: (a[key] && a[key].uplift || 0) + uplift
    })}
  }, {})
)

let inputs = [
  {Daypart: "G_POSTPEAK", day_of_week: "Monday", uplift: 1},
  {Daypart: "A_BREAKFAST", day_of_week: "Thursday", uplift: 1},
  {Daypart: "C_DAYTIME", day_of_week: "Sunday", uplift: 2},
  {Daypart: "G_POSTPEAK", day_of_week: "Monday", uplift: 2},
]

console .log (
  combine (inputs)
) 

答案 4 :(得分:0)

您可以使用loadash包以优化的方式执行此操作...

let _ = require('lodash');
let arrays = [
  {
    'name':'monday',
    vaue:1
  },
 {
    'name':'monday',
    vaue:2
 },
 {
    'name':'tuesday',
    vaue:3
 },
 {
   'name':'wednesday',
   vaue:11
 },
 {
   'name':'wednesday',
   vaue:11
 },
]

let newarray = _(arrays).groupBy("name").map(   (name) => ({
   name: name,
   value0: _.sumBy(name, 'vaue')
 }))
 .value()
console.log(newarray);