代码之家  ›  专栏  ›  技术社区  ›  Kim

对对象数组中的值求和

  •  -1
  • Kim  · 技术社区  · 7 年前
    [
    {
        "id": {
        "extId": "112",
        "year": "2000"
        },
        "Count": 1
    },
    {
        "id": {
        "extId": "113",
        "year": "2001"
        },
        "Count": 446
    },
    {
        "id": {
        "extId": "115",
        "year": "2000"
        },
        "Count": 742
    }, ...
    ]
    

    我有一个很长的对象数组。我需要根据年份来计算总数。例如,我想要类似[{2000:743},{2001:446},…]的东西。 我不知道如何在javascript中继续。我是否应该遍历数组中的每个对象并检查年份,或者是否有一些javascript函数可以简化这一过程。

    谢谢

    7 回复  |  直到 7 年前
        1
  •  2
  •   Máté Safranka    7 年前

    您可以使用 Array.reduce() :

    let countByYear = objects.reduce((acc, next) => {
        acc[next.id.year] = (acc[next.id.year] || 0) + next.Count;
        return acc;
    }, {});
    

    注意,这将产生与您的示例不同的结构(因为我对您的问题读得太草率):

    {
        2000: 743,
        2001: 446
    }
    

    然而,我想说这比 [ { 2000: 743 }, { 2001: 446 } ] ,因为在这种情况下,您有一个对象数组,每个对象都有一个键,您无法知道该键是什么,我想这会使迭代它们变得非常困难。

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

        2
  •  1
  •   Dusan Jovanov    7 年前

    您可以使用reduce:

    arr.reduce((result, current) => { result.push({[current.year]: current.Count}); return result }, [])

    这将给你这个结构[{2000:743},{2001:44}],你甚至可以 arr.filter(filterFn) 首先,如果只需要筛选特定年份

        3
  •  1
  •   Nina Scholz    7 年前

    你可以使用 Map 并获取对象数组的键/值。

    var data = [{ id: { extId: "112", year: "2000" }, Count: 1 }, { id: { extId: "113", year: "2001" }, Count: 446 }, { id: { extId: "115", year: "2000" }, Count: 742 }],
        count = Array.from(
            data.reduce(
                (m, { id: { year }, Count }) => m.set(year, (m.get(year) || 0) + Count),
                new Map
            ),
            ([year, count]) => ({ [year]: count })
        );
        
    console.log(count);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
        4
  •  1
  •   Script Host    7 年前
    <script>
      var arr=[
    {
        "id": {
        "extId": "112",
        "year": "2000"
        },
        "Count": 1
    },
    {
        "id": {
        "extId": "113",
        "year": "2001"
        },
        "Count": 446
    },
    {
        "id": {
        "extId": "115",
        "year": "2000"
        },
        "Count": 742
    }
    ];
    var result=arr.reduce((result, current) => {
      result.push({[current.id.year]: current.Count});
      return result;
    }, []);
    console.log(result);
    
    </script>
    
        5
  •  1
  •   Ankit Agarwal    7 年前

    reduce 将在此处为您完成以下操作:

    var arr = [
    {
        "id": {
        "extId": "112",
        "year": "2000"
        },
        "Count": 1
    },
    {
        "id": {
        "extId": "113",
        "year": "2001"
        },
        "Count": 446
    },
    {
        "id": {
        "extId": "115",
        "year": "2000"
        },
        "Count": 742
    },
    {
        "id": {
        "extId": "116",
        "year": "2001"
        },
        "Count": 44
    }
    ];
    
    let count = arr.reduce((acc, next) => {
        acc[next.id.year] = (acc[next.id.year] || 0) + next.Count;
        return acc;
    }, {});
    
    console.log(count);
        6
  •  1
  •   Community CDub    5 年前

    ES6

    你可以使用 reduce() 函数以获得所需的结果。

    演示

    const data = [{"id": {"extId": "112","year": "2000"},"Count": 1},{"id": {"extId": "113","year": "2001"},"Count": 446},{"id": {"extId": "115","year": "2000"},"Count": 742}];
    
    let result = data.reduce((r, {Count,id: {year}}) => {
      r[year] = (r[year] || 0) + Count;
      return r;
    }, {});
    
    console.log([result])
    .as-console-wrapper {max-height: 100% !important;top: 0;}
        7
  •  0
  •   Rahul Bisht    7 年前
    var yearCount={};
    var temp=[
    {
        "id": {
        "extId": "112",
        "year": "2000"
        },
        "Count": 1
    },
    {
        "id": {
        "extId": "113",
        "year": "2001"
        },
        "Count": 446
    },
    {
        "id": {
        "extId": "115",
        "year": "2000"
        },
        "Count": 742
    }
    ];
    temp.forEach(item=>{
        var val=yearCount[item.id.year];
      if (val){
            yearCount[item.id.year]=val+item.Count;
      }
      else{
        yearCount[item.id.year]=item.Count;
      }
    })
    console.log(yearCount);