我有一个 JSON 数组具有以下数据结构
"Jobs":
[
{ "id": "1", "JobTitle": "Engineer", "PID": "null" },
{ "id": "2", "JobTitle": "Project Manager", "PID": "null" },
{ "id": "5", "JobTitle": "Auditing Manager", "PID": "2" },
{ "id": "7", "JobTitle": "Auditor", "PID": "5" },
{ "id": "6", "JobTitle": "QA Manager", "PID": "5" },
{ "id": "3", "JobTitle": "QA", "PID": "6" },
{ "id": "4", "JobTitle": "Business Analyst", "PID": "2" }
]
我想使用 Jquery 和 Knockoutjs(可选)编写一个 java 脚本来使用 javascript 和 html 构建团队结构(组织),我有 1000 条记录,我尝试了许多递归循环来处理它,但没有成功。
输出应该是这样的
<ul id="root">
<li>Engineer</li> //since their pid is null, then they are root nodes ( yeah not only root)
<li>Project Manager</li>
<ul>
<li>Auditing Manager</li>
<li>Business Analyst</li>
</ul>
等等,它应该处理许多级别(深度),有人会建议 DFS 或 BFS 但我无法成功实现它们。