Javascript 树到深度树的转换

Javascript 树到深度树的转换,javascript,object,tree,traversal,tree-traversal,Javascript,Object,Tree,Traversal,Tree Traversal,我正在努力实现的示例: 例1 var input = { // depth of tree is 1 a: 'a' } expect(toDepthTree(input)).to.deep.equal(output) var output = { 1: { // 1 equals to depth of input a: 'a' } } 例2: var input = { // depth of tree is 2 a: { b: 'b' } } ex

我正在努力实现的示例: 例1

var input = { // depth of tree is 1
  a: 'a'
}

expect(toDepthTree(input)).to.deep.equal(output)

var output = {
  1: { // 1 equals to depth of input
    a: 'a'
  }
}
例2:

var input = { // depth of tree is 2
  a: {
    b: 'b'
  }
}

expect(toDepthTree(input)).to.equal(output)

var output = {
  2: { // 2 equals to depth of input
    a: {
      b: 'b'
    }    
  }
}
这些是更容易理解问题的介绍性示例。这是失败的实际测试:

it('should return depth tree if normal tree is provided', () => {
  const input = {
    a1: {
      b1: {
        c1: {
          d1: 'd1',
          d2: 'd2'
        },
        c2: {
          d3: 'd3'
        }
      },
      b2: {
        c3: 'c3'
      }
    },
    a2: {
      b3: 'b3'
    },
    a3: 'a3'
  }
  const output = {
    1: {
      a3: 'a3'
    },
    2: {
      a2: {
        b3: 'b3'
      }
    },
    3: {
      a1: {
        b1: {
          c2: {
            d3: 'd3'
          }
        }
      }
    }
    4: {
      a1: {
        b1: {
          c1: {
            d1: 'd1',
            d2: 'd2'
          }
        },
        b2: {
          c3: 'c3'
        }
      }
    }
  }
  expect(toLengthTree(input)).to.deep.equal(output)
})
我有以下代码来执行树遍历和返回值:

function depthOf(tree) {
  let level = 1
  for (const key in tree) {
    if (!tree.hasOwnProperty(key)) continue
    if (tree[key] && tree[key].constructor === Object) {
      const depth = depthOf(tree[key]) + 1
      level = Math.max(depth, level) // this is a problem
    }
  }
  return level
}

function* iterateTree(tree) {
  for (const key in tree) {
    if (tree.hasOwnProperty(key)) {
      const value = { [key]: tree[key] }
      yield { value, depth: depthOf(value) }
    }
  }
}

export function toDepthTree(tree) {
  const result = {}
  for (const { value, depth } of iterateTree(tree)) {
    if (!result[depth]) {
      result[depth] = {}
    }
    result[depth] = {
      ...result[depth],
      ...value
    }
  }
  return result
}
但是,最后一次测试失败,因为我得到了一个键的最大深度,因此我得到的结果是:

  const output = {
    1: {
      a3: 'a3'
    },
    2: {
      a2: {
        b3: 'b3'
      }
    },
    4: { // notice no 3: { ... } here, but it should be
      a1: {
        b1: {
          c1: {
            d1: 'd1',
            d2: 'd2'
          },
          c2: {
            d3: 'd3'
          }
        },
        b2: {
          c3: 'c3'
        }
      }
    }
  }
我正在努力寻找一种性能更好的方法来实现这一点,而不需要创建新的对象或数组,只需要使用迭代和递归


感谢您的帮助

您遇到的问题是一个问题,然后停止迭代。在这种情况下,只得到4的深度,而不是3的深度

也许这会有所帮助,它会在到达树的末尾时覆盖结果集

功能go(o、base、l){
如果(o的类型=='object'){
Object.keys(o).forEach(函数(k){
返回go(o[k],基准,l+1);
});
}否则{
结果[l]=基数;
}
}
变量输入={a1:{b1:{c1:{d1:'d1',d2:'d2'},c2:{d3:'d3'},b2:{c3:'c3'},a2:{b3:'b3'},a3:'a3'},
结果={};
Object.keys(输入).forEach(函数(k){
返回go(输入[k],输入[k],1);
});

document.write(“”+JSON.stringify(结果,0,4)+“”)您遇到的问题是一个问题,然后停止迭代。在这种情况下,只得到4的深度,而不是3的深度

也许这会有所帮助,它会在到达树的末尾时覆盖结果集

功能go(o、base、l){
如果(o的类型=='object'){
Object.keys(o).forEach(函数(k){
返回go(o[k],基准,l+1);
});
}否则{
结果[l]=基数;
}
}
变量输入={a1:{b1:{c1:{d1:'d1',d2:'d2'},c2:{d3:'d3'},b2:{c3:'c3'},a2:{b3:'b3'},a3:'a3'},
结果={};
Object.keys(输入).forEach(函数(k){
返回go(输入[k],输入[k],1);
});
document.write(“”+JSON.stringify(结果,0,4)+“”)