【问题标题】:Get sum from the nested array of objects从嵌套的对象数组中获取总和
【发布时间】:2021-07-21 07:40:46
【问题描述】:

我有以下对象数组。如您所见,每个对象中都有一个名为count 的嵌套对象。我想得到ClosedVerifyAnalyze的总数

例如Closed 的总数为 23,Verify 为 3,Analyze 为 20

"byPerson": [
      {
        "personId": "973567318",
        "firstName": "Others",
        "lastName": "",
        "count": {
          "Closed": 7,
          "Verify": 3,
          "Analyze": 19
        }
      },
      {
        "personId": "1514903899",
        "firstName": "Yatish",
        "lastName": "Patel",
        "count": {
          "Closed": 16,
          "Analyze": 1
        }
      }
 ]

我尝试过这种方法,但没有奏效。例如获得Closed

cosnt result = data.reduce((a, b) => ({"Closed": a.count["Closed"] + b.count["Closed"]}));

【问题讨论】:

    标签: javascript arrays reactjs loops object


    【解决方案1】:

    const byPerson = [
          {
            "personId": "973567318",
            "firstName": "Others",
            "lastName": "",
            "count": {
              "Closed": 7,
              "Verify": 3,
              "Analyze": 19
            }
          },
          {
            "personId": "1514903899",
            "firstName": "Yatish",
            "lastName": "Patel",
            "count": {
              "Closed": 16,
              "Analyze": 1
            }
          }
     ];
    const sum = byPerson.reduce((agg, item) => {
      ['Closed', 'Verify', 'Analyze'].forEach(f => agg[f] += item.count[f] || 0);
      return agg;
    }, {Closed: 0, Verify: 0, Analyze: 0});
    console.log(sum);

    【讨论】:

      【解决方案2】:

      使用要汇总的嵌套属性的键将数据简化为对象。在 reduce 回调中解构“totals”属性和嵌套的count 属性并计算总和。

      const data = [
        {
          "personId": "973567318",
          "firstName": "Others",
          "lastName": "",
          "count": {
            "Closed": 7,
            "Verify": 3,
            "Analyze": 19
          }
        },
        {
          "personId": "1514903899",
          "firstName": "Yatish",
          "lastName": "Patel",
          "count": {
            "Closed": 16,
            "Analyze": 1
          }
        }
      ];
      
      const totalCount = data.reduce(({
        Closed,
        Analyze,
        Verify
      }, {
        count
      }) => ({
        Closed: Closed + (count?.Closed || 0),
        Analyze: Analyze + (count?.Analyze || 0),
        Verify: Verify + (count?.Verify || 0),
      }), {
        Closed: 0,
        Analyze: 0,
        Verify: 0,
      });
      
      console.log(totalCount);

      【讨论】:

      • Resse-我运行了代码 sn-p 但它给了我错误。
      • @ArenTrot Ack,“整洁”按钮改变了语法。很抱歉,现在应该修复了。
      • Resse- 当我使用这段代码时,它告诉我 data.reduce 不是一个函数
      • 返回 23、20、0,而不是 23、20、3
      • @ArenTrot 我在我的 sn-p 中将数组命名为 data,它在您的实际代码中可能有不同的名称。
      【解决方案3】:

      在这种情况下,您甚至不需要 reduce 并且可以使用简单的循环以及迭代 Object.keys()

      const byPerson = [
            {
              "personId": "973567318",
              "firstName": "Others",
              "lastName": "",
              "count": {
                "Closed": 7,
                "Verify": 3,
                "Analyze": 19
              }
            },
            {
              "personId": "1514903899",
              "firstName": "Yatish",
              "lastName": "Patel",
              "count": {
                "Closed": 16,
                "Analyze": 1
              }
            }
       ];
       
      const sum =  {Closed: 0, Verify: 0, Analyze: 0}
      
      byPerson.forEach(({count})=> {
         Object.keys(sum).forEach(k => sum[k] += (count[k] || 0))
      });
       
      
      console.log(sum);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-15
        • 2021-11-29
        • 2013-10-11
        • 1970-01-01
        • 2021-09-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多