Javascript对象月份对应值计算

Javascript Object month corresponding value calculation

想要获取每个重复月份的总数,并保留此对象数组中唯一月份的值

[
{num: 5000, month: "September"},
{num: 6000, month: "September"},
{num: 4500, month: "August"},
{num: 3500, month: "August"},
{num: 5000, month: "jan"},
{num: 6000, month: "feb"}

]

预期输出

[
{num: 11000, month: "September"},
{num: 8000, month: "August"},
{num: 5000, month: "jan"},
{num: 6000, month: "feb"}

]

使用reduce.

const input = [
    {num: 5000, month: "September"},
    {num: 6000, month: "September"},
    {num: 4500, month: "August"},
    {num: 3500, month: "August"},
    {num: 5000, month: "jan"},
    {num: 6000, month: "feb"}  
];

const output = Object.values(input.reduce((a, {num, month}) => {
    if(!a[month]) a[month] = {num, month};
    else { a[month].num += num;}
    
    return a;
}, {}));

console.log(output);

--编辑--

const input = [
    {num: 5000, month: "September"},
    {num: 6000, month: "September"},
    {num: 4500, month: "August"},
    {num: 3500, month: "August"},
    {num: 5000, month: "jan"},
    {num: 6000, month: "feb"}  
];

const output = Object.values(input.reduce((a, {num, month}) => {
    if(!a[month]) a[month] = {num, month, count: 1};
    else { 
        a[month].num += num;
        a[month].count += 1;
    }
    
    return a;
}, {}));

console.log(output);

使用 reduce 进行初始缩小,然后将其转换回原始形状。

可以将其合并为一个,但发现作为两个单独的步骤更容易理解。

const months = [{
    num: 5000,
    month: "September"
  },
  {
    num: 6000,
    month: "September"
  },
  {
    num: 4500,
    month: "August"
  },
  {
    num: 3500,
    month: "August"
  },
  {
    num: 5000,
    month: "jan"
  },
  {
    num: 6000,
    month: "feb"
  }

]

const reduced = months.reduce((result, month) => {

  if (result[month.month]) {
    result[month.month] += month.num
  } else {
    result[month.month] = month.num
  }

  return result;
}, {})

const results = Object.keys(reduced).map(key => ({
  month: key,
  num: reduced[key]
}))

console.log(results)

您可以 reduce 数组 objectmonth 作为 key,将 num 作为 value,然后 map Object.entries 到所需的数组:

const arr = [
  { num: 5000, month: "September" },
  { num: 6000, month: "September" },
  { num: 4500, month: "August" },
  { num: 3500, month: "August" },
  { num: 5000, month: "jan" },
  { num: 6000, month: "feb" }
];

const reduced = arr.reduce((acc, { num, month }) => {
  acc[month] = (acc[month] || 0) + num;
  return acc;
}, {});

const result = Object.entries(reduced).map(([month, num]) => ({ num, month }));

console.log(result);

或者reduce直接到想要的数组:

const arr = [
  { num: 5000, month: "September" },
  { num: 6000, month: "September" },
  { num: 4500, month: "August" },
  { num: 3500, month: "August" },
  { num: 5000, month: "jan" },
  { num: 6000, month: "feb" }
];

const result2 = arr.reduce((acc, curr) => {
  const ndx = acc.findIndex(e => e.month === curr.month);

  if (ndx > -1) {
    acc[ndx].num += curr.num;
  } else {
    acc.push(curr);
  }
  return acc;
}, []);

console.log(result2)

var container = [
{num: 5000, month: "September"},
{num: 6000, month: "September"},
{num: 4500, month: "August"},
{num: 3500, month: "August"},
{num: 5000, month: "jan"},
{num: 6000, month: "feb"}
];

let result = container.reduce((acc, c) => {
    let index = acc.findIndex((v) => v.month === c.month);
    index > -1 ? acc[index].num += c.num : acc.push(c);
    return acc;
}, []);
console.log(result);

您可以为此使用 reduce 函数。