将嵌套对象转换为另一棵树

transform nested object into another tree

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

预期输出

[
    {
        "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 字段。 在下面的示例中,我们有一个由 values 键及其子项组成的对象,即 other。在深处,有 no resource 键只有 values 键,这意味着不会有子数组。

让我们看看输入格式及其结构。在data对象中,有键down和它的值,是一个对象。在那个对象里面有 valuesother。不是 values 的密钥被放入子项中。

给定输入

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,
          }
      }
  }
}

我试过的

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

使用递归:

function to_tree(d, c = []){
   var r = {false:[], true:[]}
   var vals = []
   var k = 0;
   for (var i of Object.keys(d)){
     r[i === 'values'].push(d[i])
   }
   for (var i of r[true]){
     vals.push({...i, 'id':[...c, k].map(x => x.toString()).join('-')});
     k++;
   }
   for (var i of r[false]){
      if (vals.length){
         vals[vals.length-1]['children'] = to_tree(i, [...c, k])
      }
      else{
         vals = [...vals, ...to_tree(i, c)]
      }
   }
   return vals
}