【问题标题】:how can I either move or calculate the sum of a deeply nested object's properties to the top level in an object array如何将深层嵌套对象的属性之和移动或计算到对象数组的顶层
【发布时间】:2021-07-18 05:49:58
【问题描述】:

我有一个如下的数组:

const collection = [
    {
      "name": "Top1",
      "data": [
        {
          "name": "shahnshah",
          "data": [
            {
              "name": "test1",
              "values": {
                "val1": 876,
                "val2": 3456
              }
            }
          ]
        },
        {
          "name": "Alex",
          "data": [
            {
              "name": "test1",
              "values": {
                "val1": 654,
                "val2": 300
              }
            },
            {
              "name": "test2",
              "values": {
                "val1": 676,
                "val2": 888
              }
            }
          ]
        }
      ]
    },
    {
      "name": "Top2",
      "data": [
        {
          "name": "shahnshah",
          "data": [
            {
              "name": "test1",
              "values": {
                "val1": 111,
                "val2": 300
              }
            }
          ]
        },
        {
          "name": "Alex",
          "data": [
            {
              "name": "test1",
              "values": {
                "val1": 100,
                "val2": 150
              }
            },
            {
              "name": "test2",
              "values": {
                "val1": 600,
                "val2": 50
              }
            }
          ]
        }
      ]
    }
  ];

我正在尝试将这些数据转换为一种格式,这样我可以在表格中显示这个数据,其中键“val1”、“val2”的总值添加到相应顶级键的顶层(即名称) .我期待的最终结果是这样的。

[
    {
        "name": "Top1",
        
        // Only these two values will be extra
        "val1": 2206, // "val1": 876 + "val1": 654 + "val1": 676
        "val2": 4644, // "val1": 3456 + "val1": 300 + "val1": 888

        "data": [
          {
            "name": "shahnshah",
            "data": [
              {
                "name": "test1",
                "values": {
                  "val1": 876,
                  "val2": 3456
                }
              }
            ]
          }
        ]
    },
    {
     // next object continued here
    }
  ]

这样我希望能够在顶部显示值的总和,并最终可以在嵌套表中显示它。

【问题讨论】:

  • 这不是免费的代码编写服务...您必须向我们展示您的尝试。
  • @Nur 谢谢你告诉我,我之前不知道这个

标签: javascript arrays typescript


【解决方案1】:

const collection = [
    {
      "name": "Top1",
      "data": [
        {
          "name": "shahnshah",
          "data": [
            {
              "name": "test1",
              "values": {
                "val1": 876,
                "val2": 3456
              }
            }
          ]
        },
        {
          "name": "Alex",
          "data": [
            {
              "name": "test1",
              "values": {
                "val1": 654,
                "val2": 300
              }
            },
            {
              "name": "test2",
              "values": {
                "val1": 676,
                "val2": 888
              }
            }
          ]
        }
      ]
    },
    {
      "name": "Top2",
      "data": [
        {
          "name": "shahnshah",
          "data": [
            {
              "name": "test1",
              "values": {
                "val1": 111,
                "val2": 300
              }
            }
          ]
        },
        {
          "name": "Alex",
          "data": [
            {
              "name": "test1",
              "values": {
                "val1": 100,
                "val2": 150
              }
            },
            {
              "name": "test2",
              "values": {
                "val1": 600,
                "val2": 50
              }
            }
          ]
        }
      ]
    }
  ];

let newCollection=collection.reduce((a,c)=>{
 
 
c.val1=c.data.reduce((a1,c1)=>{  return a1+c1.data.reduce((aa,cc)=> aa+cc.values.val1,0);},0)
c.val2=c.data.reduce((a1,c1)=>{  return a1+c1.data.reduce((aa,cc)=> aa+cc.values.val2,0);},0)
 

a=[...a,c];
return a;
},[])

 
console.log(newCollection);
 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-17
    • 2021-09-19
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2020-12-15
    相关资源
    最近更新 更多