我需要通過 id 將鏈接對象的數組重組為一個樹對象。深度級別未知,因此我認為應該遞歸完成。什么是最有效的方法?我有下一個對象數組:const arrObj = [ { "id": 1, "children": [ { "id": 2 }, { "id": 3 } ] }, { "id": 2, "children": [ { "id": 4 }, { "id": 5 } ] }, { "id": 3, "children": [ { "id": 6 } ] }, { "id": 4 }]我想重組只有一個對象,比如一棵樹:const treeObj = { "id": 1, "children": [ { "id": 2, "children": [ { "id": 4 }, { "id": 5 } ] }, { "id": 3, "children": [ { "id": 6 } ] } ]}每個對象都有其他許多屬性。
重組對象數組的最佳方法
飲歌長嘯
2022-12-22 15:45:05