我一直在try 将嵌套对象转换为另一个树对象,但没有成功.我想不出它的解决方案,所以任何帮助都将不胜感激.

Expected output

[
    {
        "duration": 2117538,
        "duration_min": 1001,
        "duration_max": 201530,
        "input": 0,
        "output": 0,
        "label": "down",
        "id": "0",
        "children": [
            {
                "duration": 211538,
                "duration_min": 1001,
                "duration_max": 201530,
                "input": 0,
                "output": 0,
                "boxes": 0,
                "label": "other",
                "id": "0-1",
                "children": [
                    {
                        "duration": 1538,
                        "duration_min": 1001,
                        "duration_max": 201530,
                        "input": 0,
                        "output": 0,
                        "boxes": 0,
                        "id": "0-1-0",
                        "label": "no resource",
                    },
                  
                ]
            }
        ]
    }
]

如您所见,对象的子对象被放入子对象中,随后添加了id字段.

让我们看看输入格式及其 struct .在data对象中,有键down及其值,这是一个对象.在这个物体里面有valuesother. keys 不是values,而是放在 children 身上.

Given input

const data = {
  "down": {
      "values": {
          "duration": 2117538,
          "duration_min": 1001,
          "duration_max": 201530,
          "input": 0,
          "output": 0,
          "boxes": 0,
      },
      "other": {
          "no resource": {
              "values": {
                  "duration": 1538,
                  "duration_min": 1001,
                  "duration_max": 201530,
                  "input": 0,
                  "output": 0,
                  "boxes": 0,
              }
          },
          "values": {
              "duration": 211538,
              "duration_min": 1001,
              "duration_max": 201530,
              "input": 0,
              "output": 0,
              "boxes": 0,
          }
      }
  }
}

What I tried

function getTransformData(object, name = 'total') {
    return Object.entries(object).map(function ([key, value]) {
      
      if (key === 'values') {
        return { ...value, label: name };
      }
      
      if (value.values) {
        return { ...value.values, label: key, children: getTransformData(value, key) };
      }

    });
}


console.log(getTransformData(data))

但这并不能满足需要.

推荐答案

问题已经解决了.

function transformNestedObject({values,...children}, id, duration) {
  duration = duration || values.duration;
  children = Object.entries(children).map(([key, value],i)=>{
      const _id = id ? id +"-" + i : ''+i;
      const durationPercent = (value.values.duration / duration * 100.0).toFixed(2) + '%';
      return {
        id: _id,
        label: key,
        durationPercent,
        ...transformNestedObject( value, _id, duration)
      }
    });
    
    if (id) {
      return {...values, children};
    }
    
    return children;
}

console.log(transformNestedObject(data));

Javascript相关问答推荐

JavaScript setSYS()是否有可能更早触发?

为什么子组件没有在reaction中渲染?

JavaScript .Click()函数不起作用

如果没有尾随斜线,托管在收件箱中的React/Vite将无法工作

从连接字符串创建客户端时,NodeJS连接到CosmosDB失败

togglePopover()不打开但不关闭原生HTML popover'

JS,当你点击卡片下方的绿色空间,而它是在它的背后转动时,

PDF工具包阿拉伯字体的反转数字

使用原型判断对象是否为类的实例

VUE 3捕获错误并呈现另一个组件

如何迭代叔父元素的子HTML元素

禁用.js文件扩展名并从目录导入隐式根index.js时,找不到NodeJS导入模块

在使用位板时,如何在Java脚本中判断Connect 4板中中柱的对称性?

当用户点击保存按钮时,如何实现任务的更改?

如何将zoom 变换应用到我的d3力有向图?

按下单键和多值

JS Animate()方法未按预期工作

使每个<;li>;元素的 colored颜色 与随机生成的 colored颜色 列表不同(不重复

如何在一个对象Java脚本中获取不同键的重复值?

当我点击一个按钮后按回车键时,如何阻止它再次被点击