如何使用JSON数据在Node.js中实现树结构,并避免重复?

dxxyhpgq  于 2023-08-08  发布在  Node.js
关注(0)|答案(1)|浏览(114)

我有一些来自mongoDB的json数据如下:

[
  {
    "_id": {
      "$oid": "1"
    },
    "name": "A Inc",
    "children": [
       {"$oid": "2"},{"$oid": "3"}],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "2"
    },
    "name": "ACS",
    "children": [{"$oid": "4"}],
    "kind": "Organisation"
  },
  {
    "_id": {
      "$oid": "3"
    },
    "name": "ABC Inc",
    "children": [],
    "kind": "Organisation"
  },
   {
    "_id": {
      "$oid": "5"
    },
    "name": "Disney",
    "children": [some other ids],
    "kind": "Channel"
  },
...
]

字符串
我不知道如何创建一个森林或多个树__(但不知道每棵树的根)__,理想情况下,我想生成一个数据结构,使用复活方法将所有这些数据链接在一起,但我面临的问题是有时它会有重复的层,像
100d1x


的字符串
有些孙子还在children中,我希望有一些算法来处理这种情况,如果id在叶子层,它不会在上层,这意味着,每个id理论上应该出现一次。
下面是我目前的实现:

const data = require("./nodes.json");

function createTree(data, rootId) {
  const root = data.find((item) => item._id.$oid === rootId);
  if (!root) return null;

  const children = root.children.map((child) => createTree(data, child.$oid));

  return {
    ...root,
    children,
  };
}

function prettyPrint(node, level = 0) {
  if (node.children) {
    node.children.forEach((child) => {
      console.log(
        `${"  ".repeat(level)} (${child.kind}) ${child.name} ${child._id.$oid}`
      );
      prettyPrint(child, level + 2);
    });
  }
}

const forest = data.map((item) => createTree(data, item._id.$oid));

prettyPrint(forest);


理想的结构应该是这样的,但我不知道12是不是根,也许有一些9912的根。唯一的方法可能是从数据构建树,然后从数据结构中找到根。


rta7y2nd

rta7y2nd1#

你的方向是对的。为避免重复,请复制您返回的根目录。一种简单的方法是解析字符串化的对象。

edit我的理解是OP的目标是找到数据中的所有“根”。这些被定义为没有父。首先找到这些,然后在每个根下递归地构建树...

const dataFromMDB = [
  {
    "_id": {
      "$oid": "58508c1c0b242ff1782ea053"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58508cd00b242ff1782ea057"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58508cdd0070c5f278bc3183"
      }
    ]
  },
  {
    "_id": {
      "$oid": "5850b8ce0070c5f278bc319d"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "5850b8e10b242ff1782ea087"
      }
    ]
  },
  {
    "_id": {
      "$oid": "5851e8340070c5f278bc31c3"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": []
  },
  {
    "_id": {
      "$oid": "58508b0f0b242ff1782ea04c"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5850b7d00b242ff1782ea082"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "5850b7e40070c5f278bc319a"
      }
    ]
  },
  {
    "_id": {
      "$oid": "5851c3470b242ff1782ea092"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "585089390b242ff1782ea045"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "585089dc0070c5f278bc316a"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "585089f60070c5f278bc316b"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58508a9a0b242ff1782ea04a"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58508ab40070c5f278bc3177"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58508cc20b242ff1782ea056"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5850b8e10b242ff1782ea087"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5851bd140070c5f278bc31a3"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5851d5900070c5f278bc31b8"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "585301be0070c5f278bc31c4"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "5850b7d00b242ff1782ea082"
      },
      {
        "$oid": "5850b7e40070c5f278bc319a"
      },
      {
        "$oid": "5850b5bb0070c5f278bc3194"
      },
      {
        "$oid": "5850b5a90b242ff1782ea080"
      }
    ]
  },
  {
    "_id": {
      "$oid": "5850892b0b242ff1782ea044"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "585089390b242ff1782ea045"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58508c0e0070c5f278bc317e"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58508c1c0b242ff1782ea053"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58508cdd0070c5f278bc3183"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58508d5a0070c5f278bc3187"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58508d760b242ff1782ea05c"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58508d810b242ff1782ea05d"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58508d8f0070c5f278bc3189"
      }
    ]
  },
  {
    "_id": {
      "$oid": "5850b5a90b242ff1782ea080"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "5850b5bb0070c5f278bc3194"
      }
    ]
  },
  {
    "_id": {
      "$oid": "583f7169ab44d8cc5ec303ce"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5850b7e40070c5f278bc319a"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5850b8890070c5f278bc319c"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "5850b89b0b242ff1782ea085"
      }
    ]
  },
  {
    "_id": {
      "$oid": "5851d4be0b242ff1782ea0a2"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "5851d4d50070c5f278bc31b4"
      }
    ]
  },
  {
    "_id": {
      "$oid": "585089020b242ff1782ea042"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "585089f60070c5f278bc316b"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58508ab40070c5f278bc3177"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58508d990070c5f278bc318a"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58508dad0b242ff1782ea05f"
      }
    ]
  },
  {
    "_id": {
      "$oid": "5850b89b0b242ff1782ea085"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58508ae90070c5f278bc317a"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58508b0f0b242ff1782ea04c"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58508bd60b242ff1782ea050"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58508ddc0b242ff1782ea062"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5851bcc40b242ff1782ea089"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "5851bd140070c5f278bc31a4"
      },
      {
        "$oid": "5851bd140070c5f278bc31a3"
      },
      {
        "$oid": "5851bd140070c5f278bc31a2"
      },
      {
        "$oid": "5851bd140070c5f278bc31a5"
      }
    ]
  },
  {
    "_id": {
      "$oid": "5851bd140070c5f278bc31a5"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5851c3260070c5f278bc31ab"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "5851c3470b242ff1782ea092"
      }
    ]
  },
  {
    "_id": {
      "$oid": "585335280070c5f278bc31c8"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "585088f10070c5f278bc3167"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "585089020b242ff1782ea042"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58508b8f0070c5f278bc317c"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": []
  },
  {
    "_id": {
      "$oid": "58508dad0b242ff1782ea05f"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5851d4d50070c5f278bc31b4"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58508c2c0b242ff1782ea054"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58508c380070c5f278bc3180"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58508dc50070c5f278bc318c"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5850b5bb0070c5f278bc3194"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "583f7169ab44d8cc5ec303cd"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58508db90b242ff1782ea060"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58508dc50070c5f278bc318c"
      }
    ]
  },
  {
    "_id": {
      "$oid": "5851bd140070c5f278bc31a4"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5851d5620b242ff1782ea0a3"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "5851d5900070c5f278bc31b8"
      }
    ]
  },
  {
    "_id": {
      "$oid": "584f16710b242ff1782ea040"
    },
    "name": "XXXXX", 
    "kind": "Site",
    "children": [
      {
        "$oid": "5a382e29b6f4474bb605466b"
      },
      {
        "$oid": "5a38287ab6f4474bb605465f"
      },
      {
        "$oid": "5941d216381e3b5854db8aa5"
      },
      {
        "$oid": "5a38287ab6f4474bb6054662"
      },
      {
        "$oid": "5941d216381e3b5854db8aa7"
      },
      {
        "$oid": "5878417eb874d4807727aa80"
      },
      {
        "$oid": "5a38287ab6f4474bb6054661"
      },
      {
        "$oid": "5941d4e7381e3b5854db8aad"
      },
      {
        "$oid": "58584e370070c5f278bc31e2"
      },
      {
        "$oid": "5941d216381e3b5854db8aa8"
      },
      {
        "$oid": "596ed4706f8b592916fc3963"
      },
      {
        "$oid": "5941d4f1381e3b5854db8aaf"
      },
      {
        "$oid": "596ed4706f8b592916fc3965"
      },
      {
        "$oid": "5941d19f381e3b5854db8aa2"
      },
      {
        "$oid": "5a38287ab6f4474bb6054663"
      },
      {
        "$oid": "596ed4706f8b592916fc3961"
      },
      {
        "$oid": "5a382e28b6f4474bb605466a"
      },
      {
        "$oid": "58784131b624487b77dfc100"
      },
      {
        "$oid": "5941d4f1381e3b5854db8ab0"
      },
      {
        "$oid": "5941d216381e3b5854db8aa6"
      },
      {
        "$oid": "5b57cd878fff7b2824987a06"
      },
      {
        "$oid": "5a38287ab6f4474bb6054660"
      },
      {
        "$oid": "5b57cd878fff7b2824987a07"
      },
      {
        "$oid": "596ed4706f8b592916fc3964"
      },
      {
        "$oid": "596ed4706f8b592916fc3962"
      },
      {
        "$oid": "585849800070c5f278bc31e0"
      },
      {
        "$oid": "5941d4f1381e3b5854db8aae"
      },
      {
        "$oid": "5941d4f1381e3b5854db8ab1"
      },
      {
        "$oid": "5941d1e4381e3b5854db8aa4"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58508bc20070c5f278bc317d"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58508bd60b242ff1782ea050"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58508c380070c5f278bc3180"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58508cb10070c5f278bc3181"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58508cc20b242ff1782ea056"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58508d4f0070c5f278bc3186"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5850af300070c5f278bc3191"
    },
    "name": "XXXXX", 
    "kind": "Site",
    "children": [
      {
        "$oid": "5851bd140070c5f278bc31a4"
      },
      {
        "$oid": "5850b7d00b242ff1782ea082"
      },
      {
        "$oid": "585301be0070c5f278bc31c4"
      },
      {
        "$oid": "5851d5620b242ff1782ea0a3"
      },
      {
        "$oid": "5851c3260070c5f278bc31ab"
      },
      {
        "$oid": "5851bd140070c5f278bc31a3"
      },
      {
        "$oid": "5850b5870b242ff1782ea07e"
      },
      {
        "$oid": "5850b8ce0070c5f278bc319d"
      },
      {
        "$oid": "5850b7e40070c5f278bc319a"
      },
      {
        "$oid": "5850b8e10b242ff1782ea087"
      },
      {
        "$oid": "5850b89b0b242ff1782ea085"
      },
      {
        "$oid": "5851bd140070c5f278bc31a2"
      },
      {
        "$oid": "5851bcc40b242ff1782ea089"
      },
      {
        "$oid": "5850b8890070c5f278bc319c"
      },
      {
        "$oid": "5851c3470b242ff1782ea092"
      },
      {
        "$oid": "5850b5bb0070c5f278bc3194"
      },
      {
        "$oid": "5851bd140070c5f278bc31a5"
      },
      {
        "$oid": "5851d4d50070c5f278bc31b4"
      },
      {
        "$oid": "5850b5a90b242ff1782ea080"
      },
      {
        "$oid": "5851d5900070c5f278bc31b8"
      },
      {
        "$oid": "5851d4be0b242ff1782ea0a2"
      }
    ]
  },
  {
    "_id": {
      "$oid": "5875a688b874d4807727aa7a"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "5875a699b624487b77dfc0f7"
      }
    ]
  },
  {
    "_id": {
      "$oid": "588805a0b624487b77dfc263"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "588805b7b624487b77dfc265"
      },
      {
        "$oid": "588805b7b624487b77dfc264"
      },
      {
        "$oid": "588805b7b624487b77dfc267"
      },
      {
        "$oid": "588805b7b624487b77dfc266"
      }
    ]
  },
  {
    "_id": {
      "$oid": "5888073bb624487b77dfc273"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "588807d0b624487b77dfc285"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5888081cb624487b77dfc288"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "588a9506b624487b77dfc2c8"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "589112ebb624487b77dfc2fa"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "589113b6b624487b77dfc31b"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5891148db624487b77dfc336"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "589a3281b624487b77dfc343"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "589a32d9b624487b77dfc344"
      }
    ]
  },
  {
    "_id": {
      "$oid": "589a3e12b624487b77dfc357"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58a255a40bb5cb35020957e7"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58a255b1994a863602070084"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58a4b3e10bb5cb3502095807"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58a4b3e80bb5cb3502095808"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58a4eaa10bb5cb3502095818"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58aa1cd3994a8636020700ca"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58aa1d8a0bb5cb3502095828"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58ab9da3994a863602070104"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58abcde1994a86360207010c"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58bdc9ae7ca25aae497a3c78"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58bdc9b97ca25aae497a3c7c"
      },
      {
        "$oid": "58bdc9b97ca25aae497a3c79"
      },
      {
        "$oid": "58bdc9b97ca25aae497a3c7a"
      },
      {
        "$oid": "58bdc9b97ca25aae497a3c7b"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58c1a0ed92704aa709c4668c"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58c1a0ed92704aa709c4668d"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58c1fa5d74df84452348fe7d"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58c20da774df84452348fea3"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58c20da774df84452348feab"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58c210fb74df84452348febd"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58c5ed164a4b9ca32bc53fd3"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58e43b33f67c5f75420cc44e"
    },
    "name": "XXXXX", 
    "kind": "Site",
    "children": [
      {
        "$oid": "58e43c26f67c5f75420cc454"
      },
      {
        "$oid": "58e43c26f67c5f75420cc457"
      },
      {
        "$oid": "58e43c26f67c5f75420cc455"
      },
      {
        "$oid": "58e43be9f79ce47642fae5c7"
      },
      {
        "$oid": "58e43bb0f67c5f75420cc450"
      },
      {
        "$oid": "58e43c00f67c5f75420cc452"
      },
      {
        "$oid": "58e43c26f67c5f75420cc456"
      },
      {
        "$oid": "58e43c16f67c5f75420cc453"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58e43c00f67c5f75420cc452"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "58ff9b860049d1ba27ff9d90"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "58ff9bdb0049d1ba27ff9d91"
      }
    ]
  },
  {
    "_id": {
      "$oid": "590131150049d1ba27ff9d98"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "590902a491391c1523bad64e"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5909159c91391c1523bad660"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "590fde37381e3b5854db89cc"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5914eb368ed1755954d6e893"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "5914eb368ed1755954d6e895"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "59377642381e3b5854db8a55"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "5937765a381e3b5854db8a56"
      }
    ]
  },
  {
    "_id": {
      "$oid": "596815f12d52383a7ed77c0b"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "596815f12d52383a7ed77c0c"
      }
    ]
  },
  {
    "_id": {
      "$oid": "596c36203a45e57a3fa2ed06"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "596d47bcb6a2a86ef4d01e38"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "596e9108c6acd437948597d6"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "596e9108c6acd437948597d7"
      },
      {
        "$oid": "596e9108c6acd437948597d8"
      },
      {
        "$oid": "596e9108c6acd437948597da"
      },
      {
        "$oid": "596e9108c6acd437948597d9"
      }
    ]
  },
  {
    "_id": {
      "$oid": "596ed4706f8b592916fc3965"
    },
    "name": "XXXXX", 
    "kind": "Channel",
    "children": []
  },
  {
    "_id": {
      "$oid": "59701b69ea371e1c198c7272"
    },
    "name": "XXXXX", 
    "kind": "Device",
    "children": [
      {
        "$oid": "59701b69ea371e1c198c7273"
      }
    ]
  }
];

// assign the data parent references
// for each node, get its children. for each child, assign
// the child's parent id to the nodeId
const assignLineage = data => {
  data.forEach(node => {
    const nodeId = node._id.$oid;
    node.children.forEach(childId => {
      const child = data.find(el => el._id.$oid == childId);
      if (child) child.parentId = nodeId;
    });
  });
};

const createTree = (data, rootId) => {
  const root = data.find(el => el._id.$oid == rootId);
  if (!root) return null;
    
  let rootCopy = JSON.parse(JSON.stringify(root));
  rootCopy.children = rootCopy.children.map(id => {
    const child = createTree(data, id.$oid);
    if (child) child.parentId = rootCopy._id.$oid;
    return child;
  }).filter(el => el);
  return rootCopy;
};

// first assign parent pointers
assignLineage(dataFromMDB)

// roots don't have parents
const roots = dataFromMDB.filter(obj => !obj.parentId)
console.log(`there are ${roots.length} nodes without a parent`)

const forest = roots.map(root => createTree(dataFromMDB, root._id.$oid))
console.log(forest);

字符串

相关问题