【问题标题】:Javascript Object month corresponding value calculationJavascript Object月份对应值计算
【发布时间】:2019-09-19 15:17:01
【问题描述】:

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

[
{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"}

]

【问题讨论】:

    标签: javascript arrays object numbers calculation


    【解决方案1】:

    使用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);

    【讨论】:

    • 是否也可以添加出现次数?喜欢[{“num”:11000,“月”:“九月”,计数:2},{“num”:8000,“月”:“八月”,计数:2},{“num”:5000,“月”:“一月”,计数:1 },{“数字”:6000,“月”:“二月”,计数:1 }]
    【解决方案2】:

    使用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)

    【讨论】:

      【解决方案3】:

      您可以将reduce 数组转换为object,将month 作为keynum 作为value,然后将mapObject.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)

      【讨论】:

        【解决方案4】:
        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 函数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-06-20
          • 1970-01-01
          • 2013-09-17
          • 2011-04-08
          • 2019-01-03
          • 2014-06-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多