本文介绍了如何从简单的json制作所需的json树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将简单的 json 转换为 json 树时遇到问题



我需要遵循的步骤是:


  1. 查找父元素 pid:0 将有一些 _id:'5a016637e986c90418e96ee9'


  2. 在其他对象 _id:5a016637e986c90418e96ee9 as pid > 例如:将导致 _id:5a0d852ce986c9041df2aafb 以及其他一些


  3. 查找高于 _id 我,e 5a0d852ce986c9041df2aafb 等等......

这是我的json:



我想将我的json转换为如下所示:

  [
{
_id:5a016637e986c90418e96ee9,
pid: 0\" ,
name:general-categories,
nodes:[
{
priority:3,
name_lower:悲剧,
_id:5a0d852ce986c9041df2aafb,
pid:b016637e986c90418e96ee9,
名称:悲剧,

节点:[
{
priority:3,
name_lower:natural,
_id:5a0d8544e986c9041df2aafd,
pid:5a0d852ce986c9041df2aafb,
name:natural,

nodes:[

{
priority:3,
name_lower :地震,
_id:5a0d85c6e986c9041df2ab02,
pid:5a0d8544e986c9041df2aafd,
名称:地震,
节点: [] //等............
}
]
}
]
}
]
}
]

在这里看到输出json我想如何转换我的json:



查看完整数据:



这是我尝试过的(有一半样本数据):



  var data = [{categories:[{_ id:5a016637e986c90418e96ee9,pid:0,name : 一般类别},{ 优先:3 name_lower: 悲剧, _ ID: 5a0d852ce986c9041df2aafb, PID:5a016 637e986c90418e96ee9\" , 名: 悲剧},{ 优先:3, name_lower: 自然, _ ID: 5a0d8544e986c9041df2aafd, PID: 5a0d852ce986c9041df2aafb, 名: 自然 },{ 优先:3, name_lower: 人类, _ ID: 5a0d8544e986c9041df2aafe, PID: 5a0d852ce986c9041df2aafb, 名: 人},{ 优先:3 name_lower : 大地震, _ ID: 5a0d85c6e986c9041df2ab02, PID: 5a0d8544e986c9041df2aafd, 名: 大地震},{ 优先:3, name_lower: 洪水, _ ID : 5a0d85c6e986c9041df2ab03, PID: 5a0d8544e986c9041df2aafd, 名: 洪水},{ 优先:3, name_lower: 干旱, _ ID: 5a0d85c6e986c9041df2ab04, PID: 5a0d8544e986c9041df2aafd, 名: 干旱},{ 优先:3, name_lower: 山体滑坡, _ ID: 5a0d85c6e986c9041df2ab05, PID: 5a0d8544e986c9041df2aafd, 名: 山体滑坡 },{ 优先:3, name_lower: 海啸, _ ID: 5a0d85c6e986c9041df2ab07, PID: 5a0d8544e986c9041df2aafd, 名: 海啸},{ 优先:3 name_lower : 森林火灾, _ ID: 5a0d85c6e986c9041df2ab09, PID: 5a0d8544e986c9041df2aafd , 名: 森林火灾},{ 优先:3, name_lower: 神池, _ ID: 5a0d85c6e986c9041df2ab0a, PID: 5a0d8544e986c9041df2aafd, 名: 神池 },{ 优先:3, name_lower: 旋风, _ ID: 5a0d85c6e986c9041df2ab0f, PID: 5a0d8544e986c9041df2aafd, 名: 旋风},{ 优先:3 name_lower : 云脉冲串, _ ID: 5a0d85c6e986c9041df2ab12, PID: 5a0d8544e986c9041df2aafd, 名称: 云脉冲串},{ 优先:3 name_lower: 油菜 , _ ID: 5a0d863ce986c9041df2ab14, PID: 5a0d8544e986c9041df2aafe, 名: 强奸},{ 优先:3, name_lower: 绑架, _ ID: 5a0d863ce986c9041df2ab15, PID : 5a0d8544e986c9041df2aafe, 名: 绑架},{ 优先:3, name_lower: 劫持, _ ID: 5a0d863ce986c9041df2ab16, PID: 5a0d8544e986c9041df2aafe, 名 : 劫持},{ 优先:3, name_lower: 恐怖袭击, _ ID: 5a0d863ce986c9041df2ab17, PID: 5a0d8544e986c9041df2aafe, 名: 恐怖攻击}, { 优先:3, name_lower: 炸弹爆炸, _ ID: 5a0d863ce986c9041df2ab18, PID: 5a0d8544e986c9041df2aafe, 名:炸弹爆炸},{ 优先 :3, name_lower : harrasment /滥用  _ ID: 5a0d863ce986c9041df2ab1c, PID: 5a0d8544e986c9041df2aafe, 名: harrasment /滥用},{ 优先3, name_lower : 嫁妆  _ ID: 5a0d863ce986c9041df2ab1d, PID: 5a0d8544e986c9041df2aafe, 名: 嫁妆},{ 优先:3, name_lower: 荣誉-killing, _ ID: 5a0d863ce986c9041df2ab20, PID: 5a0d8544e986c9041df2aafe, 名: 名誉杀人},{ 优先:3, name_lower: 谋杀, _ ID: 5a0d863ce986c9041df2ab21, PID: 5a0d8544e986c9041df2aafe, 名: 谋杀},{ 优先:3, name_lower: sucide, _ ID: 5a0d8652e986c9041df2ab26, PID:5a0d8544e986c9041df2aafe , 名: sucide},{ 优先:3, name_lower: 道路, _ ID: 5a0d8670e986c9041df2ab28, PID: 5a016637e986c90418e96ee9, 名: 道路} { 优先:3, name_lower: 交通, _ ID: 5a0d868ce986c9041df2ab2a, PID: 5a0d8670e986c9041df2ab28, 名: 交通},{ 优先:3,name_lower : 状态, _ ID: 5a0d868ce986c9041df2ab2b, PID: 5a0d8670e986c9041df2ab28, 名: 状态},{ 优先:3 name_lower: 国家, _ ID: 5a0d868ce986c9041df2ab2c, PID: 5a0d8670e986c9041df2ab28, 名: 国家},{ 优先:3, name_lower: 预算, _id : 5a0d868ce986c9041df2ab2d, PID: 5a0d8670e986c9041df2ab28, 名: 预算},{ 优先:3, name_lower: 意外, _ ID: 5a0d868ce986c9041df2ab2e, PID : 5a0d8670e986c9041df2ab28, 名: 意外},{ 优先:3, name_lower: 贩卖, _ ID: 5a0d869fe986c9041df2ab34, PID: 5a0d8544e986c9041df2aafe, 名:贩卖 },{ 优先 :3, name_lower : 动物, _ ID: 5a0d86aae986c9041df2ab36, PID: 5a0d869fe986c9041df2ab34, 名: 动物},{ 优先:3 name_lower: 人, _ ID: 5a0d86aae986c9041df2ab37, PID: 5a0d869fe986c9041df2ab34, 名: 人},{ 优先:3, name_lower: 铁路, _id : 5a0d86c9e986c9041df2ab3a, PID: 5a016637e986c90418e96ee9, 名: 铁路},{ 优先:3, name_lower: 北, _ ID: 5a0d86d6e986c9041df2ab3c, PID : 5a0d86c9e986c9041df2ab3a, 名: 北},{ 优先:3, name_lower: 东, _ ID : 5a0d86d6e986c9041df2ab3d, PID: 5a0d86c9e986c9041df2ab3a, 名: 东},{ 优先:3, name_lower: 南, _ ID: 5a0d86d6e986c9041df2ab3e, PID: 5a0d86c9e986c9041df2ab3a, 名: 南},{ 优先:3, name_lower: 西部大开发, _ ID: 5a0d86d6e986c9041df2ab3f, PID: 5a0d86c9e986c9041df2ab3a, 名: 西 },{ 优先:3, name_lower: 中央, _ ID: 5a0d86d6e986c9041df2ab40, PID: 5a0d86c9e986c9041df2ab3a, 名: 中央},{ 优先:3 name_lower : 预算, _ ID: 5a0d86e2e986c9041df2ab46, PID: 5a0d86c9e986c9041df2ab3a, 名: 预算},{ 优先:3, name_lower: 自然, _ ID : 5a12c1c7e986c90418cefdfd, PID: 5a016637e986c90418e96ee9, 名: 自然},{ 优先:3, name_lower: 动物, _ ID: 5a12c1d8e986c90418cefdff, PID: 5a12c1c7e986c90418cefdfd, 名: 动物},{ 优先:3, name_lower: 人类, _ ID: 5a12c1d8e986c90418cefe00, PID: 5a12c1c7e986c90418cefdfd, 名: 人 },{priority:3,name_lower:两栖动物,_ id:5a12c277e986c90418cefe03,pid:5a12c 1d8e986c90418cefdff,name:两栖动物},{priority:3,name_lower:tresstrial animals,_ id:5a12c277e986c90418cefe04,pid:5a12c1d8e986c90418cefdff,name: Tresstrial animals},{priority:3,name_lower:aquatic animals,_ id:5a12c277e986c90418cefe05,pid:5a12c1d8e986c90418cefdff,name:Aquatic animals},{priority :3,name_lower:arial animals,_ id:5a12c277e986c90418cefe06,pid:5a12c1d8e986c90418cefdff,name:Arial animals},{priority:3,name_lower: 儿童, _ ID: 5a12c2c7e986c90418cefe0f, PID: 5a12c1d8e986c90418cefe00, 名: 孩子},{ 优先:3, name_lower: 宝贝, _ ID:5a12c2c7e986c90418cefe10 , PID: 5a12c1d8e986c90418cefe00, 名: 宝贝},{ 优先:3, name_lower: 成人, _ ID: 5a12c2c7e986c90418cefe11, PID: 5a12c1d8e986c90418cefe00 名: 成人},{ 优先:3, name_lower: 女, _ ID: 5a12c2d4e986c90418cefe15, PID: 5a12c2c7e986c90418cefe11, 名: 女},{ 优先:3, name_lower: 男, _ ID: 5a12c2d4e986c90418cefe16,p ID : 5a12c2c7e986c90418cefe11\" , 名: 男},{ 优先:3, name_lower: 男, _ ID: 5a12c2d9e986c90418cefe19, PID: 5a12c2c7e986c90418cefe10, 名 : 男},{ 优先:3, name_lower: 女, _ ID: 5a12c2d9e986c90418cefe1a, PID: 5a12c2c7e986c90418cefe10, 名: 女},{ 优先 3, name_lower: 女, _ ID: 5a12c2dde986c90418cefe1d, PID: 5a12c2c7e986c90418cefe0f, 名: 女},{ 优先:3, name_lower: 男 , _ ID: 5a12c2dde986c90418cefe1e, PID: 5a12c2c7e986c90418cefe0f, 名: 男},{ 优先:3, name_lower: 政治, _ ID: 5a12c2ebe986c90418cefe21, PID : 5a016637e986c90418e96ee9\" , 名: 政治学},{ 优先:3, name_lower: 人民党, _ ID: 5a12c321e986c90418cefe23, PID: 5a12c2ebe986c90418cefe21, 名 : 人民党},{ 优先:3, name_lower: aidmk, _ ID: 5a12c321e986c90418cefe24, PID: 5a12c2ebe986c90418cefe21, 名: AIDMK},{ 优先 3, name_lower: BSP, _ ID: 5a12c321e986c90418cefe25, PID: 5a12c2ebe986c90418cefe21, 名: BSP},{ 优先:3,名_下 : 十七大, _ ID: 5a12c321e986c90418cefe26, PID: 5a12c2ebe986c90418cefe21, 名: 国会},{ 优先:3, name_lower: AAP, _ ID : 5a12c321e986c90418cefe27, PID: 5a12c2ebe986c90418cefe21, 名: AAP},{ 优先:3, name_lower: DMK, _ ID: 5a12c321e986c90418cefe28, PID: 5a12c2ebe986c90418cefe21\" , 名: DMK},{ 优先:3, name_lower: 其他, _ ID: 5a12c322e986c90418cefe2b, PID: 5a12c2ebe986c90418cefe21, 名: 其他 },{ 优先:3, name_lower: 政府, _ ID: 5a12c32fe986c90418cefe31, PID: 5a016637e986c90418e96ee9, 名: 政府},{ 优先:3 name_lower:州政府,_ id:5a12c33be986c90418cefe33,pid:5a12c32fe986c90418cefe31,name:州政府},{priority:3,name_lower:中央政府, _id:5a12c33be986c90418cefe34,pid:5a12c32fe986c90418cefe31,name:中央政府},{priority:3,name_lower:总统规则,_ id:5a12c361e98​​6c90418cefe39, pid:5a12c32fe986c90418cefe31,name:总统统治},{priori TY :3, name_lower : 总统  _ ID: 5a12c38fe986c90418cefe3b, PID: 5a12c33be986c90418cefe34, 名: 总统},{ 优先:3, name_lower:选举  _ ID: 5a12c38fe986c90418cefe3c, PID: 5a12c33be986c90418cefe34, 名: 选举},{ 优先:3, name_lower: 总理, _ ID: 5a12c38fe986c90418cefe3d  PID: 5a12c33be986c90418cefe34, 名: 总理},{ 优先:3, name_lower: parilament, _ ID: 5a12c38fe986c90418cefe3e, PID: 5a12c33be986c90418cefe34\" , 名: Parilament},{ 优先:3, name_lower: MPS, _ ID: 5a12c390e986c90418cefe3f, PID: 5a12c33be986c90418cefe34, 名: MPS },{ 优先:3, name_lower: 工作重点, _ ID: 5a12c390e986c90418cefe40, PID: 5a12c33be986c90418cefe34, 名: 工作重点},{ 优先:3 name_lower : 家部长  _ ID: 5a12c3d5e986c90418cefe47, PID: 5a12c33be986c90418cefe33, 名: 家部长},{ 优先:3, name_lower: railway-部长, _ ID: 5a12c3d5e986c90418cefe49, PID: 5a12c33be986c90418cefe33, 名: 铁路部长 },{ 优先:3, name_lower: MPS, _ ID: 5a12c3d5e986c90418cefe4a, PID: 5a12c33be986c90418cefe33, 名: 国会议员},{ 优先:3 name_lower : 总督, _ ID: 5a12c3d5e986c90418cefe4b, PID: 5a12c33be986c90418cefe33, 名: 都督},{ 优先:3, name_lower: 选举, _ ID : 5a12c3d5e986c90418cefe4c, PID: 5a12c33be986c90418cefe33, 名: 选举},{ 优先:3, name_lower: 工作重点, _ ID: 5a12c3d5e986c90418cefe4d, PID: 5a12c33be986c90418cefe33\" , 名: 工作重点},{ 优先:3, name_lower: cheif部长, _ ID: 5a12c3d5e986c90418cefe4e, PID: 5a12c33be986c90418cefe33, 名: Cheif-Minister}]}]函数getParentObject(parentObj){var allParentObject = parentObj.filter(function(o){return o.parent == 0; }); return allParentObject;} function getAllParentIds(allParentObject){var allActualParentIds = allParentObject.map(obj => obj._id); return allActualParentIds;} function getAllSubChilds(subchild){var subChildObject = subchild.filter(function(o,i){return o._id == subchild [i] ._ id;}); return subChildObject; function findNestedObjects(data,allParentObject,ActualParentId){var firstChild = getParentObject(data); var allActualParentIds = getAllParentIds(firstChild); var obj = {}; //console.log('all childs',JSON.stringify(firstChild)); if(firstChild.length){// allParentObject.nodes = firstChild; var subChildObject = getAllSubChilds(firstChild); console.log('sub child object',JSON.stringify(subChildObject)); var subChildIds = getAllParentIds(subChildObject); //onsole.log('所有父节点',JSON.stringify(allParentObject)); // findNestedObjects(数据,则firstChild,ActualParentId); } else {allParentObject.nodes = []; var allParentObject = getParentObject(data); var allActualParentIds = getAllParentIds(allParentObject); for(var i = 0; i< allActualParentIds.length; i ++){findNestedObjects(data,allParentObject [i],allActualParentIds [i]); }  
 < script src =https:// ajax .googleapis.com / AJAX /库/ jquery的/ 2.1.1 / jquery.min.js>< /脚本>  



查看完整数据:



请提前帮助我!

解决方案

这是一个简单的递归算法,用于构建这个树(我创建了一个简化的数据版本,但一切似乎都运行良好):



  function setNodes(obj,data){obj.nodes = data.filter( x => x.pid === obj._id);} function populateLevel(arr,data){arr.forEach(x => { setNodes(x,data); if(x.nodes.length){populateLevel(x.nodes,data); }}};} const json = [{categories:[{_ id:1,pid:0,name:general-categories},{priority:3, name_lower:悲剧,_ id:2,pid:1,名称:悲剧},{优先级:3,name_lower:自然,_ id :3,pid:1,name:natural},{priority:3,name_lower:human,_ id:4,pid: 2,名称:人类},{优先级:3,name_lower:地震,_ id:5,pid:2,名称:地震},{priority:3,name_lower:floods,_ id:6,pid:3,name:floods},{priority:3, name_lower:干旱,_ id:7,pid:3,名称:干旱},{优先级:3,name_lower:山体滑坡,_ id :8,pid:6,name:landslides},{priority:3,name_lower:tsunami,_ id:9, pid:6,name:tsunami}]}]; const data = json [0] .categories; const startingLevel = data.filter(x => x.pid ==='0'); populateLevel(startingLevel,data); console.log(startingLevel);  



这是一个jsfiddle:


I have problem in converting simple json into json tree

The steps i need to follow are:

  1. finding parent having pid:0 will have some _id:'5a016637e986c90418e96ee9'

  2. finding above _id:5a016637e986c90418e96ee9 as pid in other object for example: will result in _id:5a0d852ce986c9041df2aafb and some others etc

  3. finding above _id i,e 5a0d852ce986c9041df2aafb so on..

Here is my json: https://jsfiddle.net/eabangalore/26gxm6dz/5/

I want to convert my json to like below one:

[
   {
    "_id": "5a016637e986c90418e96ee9",
    "pid": "0",
    "name": "general-categories",
    "nodes":[
         {
            "priority": 3,
            "name_lower": "tragedy",
            "_id": "5a0d852ce986c9041df2aafb",
            "pid": "5a016637e986c90418e96ee9",
            "name": "Tragedy",

          "nodes":[
                {
                "priority": 3,
                "name_lower": "natural",
                "_id": "5a0d8544e986c9041df2aafd",
                "pid": "5a0d852ce986c9041df2aafb",
                "name": "natural",

                "nodes":[

                   {
                      "priority": 3,
                      "name_lower": "earthquake",
                      "_id": "5a0d85c6e986c9041df2ab02",
                      "pid": "5a0d8544e986c9041df2aafd",
                      "name": "earthquake",
                      "nodes":[]  // so on ............
                    }
                ]
              }
          ]
        }
    ]
  }
]

see the output json here this how i want to convert my json:https://jsfiddle.net/Resendra/0uzmoy34/

see fiddle with full data: https://jsfiddle.net/eabangalore/fzonzxjt/73/

this is what i have tried(with half sample data):

var data = [{"categories":[{"_id":"5a016637e986c90418e96ee9","pid":"0","name":"general-categories"},{"priority":3,"name_lower":"tragedy","_id":"5a0d852ce986c9041df2aafb","pid":"5a016637e986c90418e96ee9","name":"Tragedy"},{"priority":3,"name_lower":"natural","_id":"5a0d8544e986c9041df2aafd","pid":"5a0d852ce986c9041df2aafb","name":"natural"},{"priority":3,"name_lower":"humans","_id":"5a0d8544e986c9041df2aafe","pid":"5a0d852ce986c9041df2aafb","name":"humans"},{"priority":3,"name_lower":"earthquake","_id":"5a0d85c6e986c9041df2ab02","pid":"5a0d8544e986c9041df2aafd","name":"earthquake"},{"priority":3,"name_lower":"floods","_id":"5a0d85c6e986c9041df2ab03","pid":"5a0d8544e986c9041df2aafd","name":"floods"},{"priority":3,"name_lower":"drought","_id":"5a0d85c6e986c9041df2ab04","pid":"5a0d8544e986c9041df2aafd","name":"drought"},{"priority":3,"name_lower":"landslides","_id":"5a0d85c6e986c9041df2ab05","pid":"5a0d8544e986c9041df2aafd","name":"landslides"},{"priority":3,"name_lower":"tsunami","_id":"5a0d85c6e986c9041df2ab07","pid":"5a0d8544e986c9041df2aafd","name":"tsunami"},{"priority":3,"name_lower":"forest-fire","_id":"5a0d85c6e986c9041df2ab09","pid":"5a0d8544e986c9041df2aafd","name":"forest-fire"},{"priority":3,"name_lower":"strom","_id":"5a0d85c6e986c9041df2ab0a","pid":"5a0d8544e986c9041df2aafd","name":"strom"},{"priority":3,"name_lower":"cyclone","_id":"5a0d85c6e986c9041df2ab0f","pid":"5a0d8544e986c9041df2aafd","name":"cyclone"},{"priority":3,"name_lower":"cloud-burst","_id":"5a0d85c6e986c9041df2ab12","pid":"5a0d8544e986c9041df2aafd","name":"cloud-burst"},{"priority":3,"name_lower":"rape","_id":"5a0d863ce986c9041df2ab14","pid":"5a0d8544e986c9041df2aafe","name":"rape"},{"priority":3,"name_lower":"kidnap","_id":"5a0d863ce986c9041df2ab15","pid":"5a0d8544e986c9041df2aafe","name":"kidnap"},{"priority":3,"name_lower":"hijack","_id":"5a0d863ce986c9041df2ab16","pid":"5a0d8544e986c9041df2aafe","name":"hijack"},{"priority":3,"name_lower":"terror-attack","_id":"5a0d863ce986c9041df2ab17","pid":"5a0d8544e986c9041df2aafe","name":"terror-attack"},{"priority":3,"name_lower":"bomb-blast","_id":"5a0d863ce986c9041df2ab18","pid":"5a0d8544e986c9041df2aafe","name":"bomb-blast"},{"priority":3,"name_lower":"harrasment/abuse","_id":"5a0d863ce986c9041df2ab1c","pid":"5a0d8544e986c9041df2aafe","name":"harrasment/abuse"},{"priority":3,"name_lower":"dowry","_id":"5a0d863ce986c9041df2ab1d","pid":"5a0d8544e986c9041df2aafe","name":"dowry"},{"priority":3,"name_lower":"honour-killing","_id":"5a0d863ce986c9041df2ab20","pid":"5a0d8544e986c9041df2aafe","name":"honour-killing"},{"priority":3,"name_lower":"murder","_id":"5a0d863ce986c9041df2ab21","pid":"5a0d8544e986c9041df2aafe","name":"murder"},{"priority":3,"name_lower":"sucide","_id":"5a0d8652e986c9041df2ab26","pid":"5a0d8544e986c9041df2aafe","name":"sucide"},{"priority":3,"name_lower":"roads","_id":"5a0d8670e986c9041df2ab28","pid":"5a016637e986c90418e96ee9","name":"Roads"},{"priority":3,"name_lower":"traffic","_id":"5a0d868ce986c9041df2ab2a","pid":"5a0d8670e986c9041df2ab28","name":"traffic"},{"priority":3,"name_lower":"state","_id":"5a0d868ce986c9041df2ab2b","pid":"5a0d8670e986c9041df2ab28","name":"state"},{"priority":3,"name_lower":"national","_id":"5a0d868ce986c9041df2ab2c","pid":"5a0d8670e986c9041df2ab28","name":"national"},{"priority":3,"name_lower":"budget","_id":"5a0d868ce986c9041df2ab2d","pid":"5a0d8670e986c9041df2ab28","name":"budget"},{"priority":3,"name_lower":"accidents","_id":"5a0d868ce986c9041df2ab2e","pid":"5a0d8670e986c9041df2ab28","name":"accidents"},{"priority":3,"name_lower":"trafficking","_id":"5a0d869fe986c9041df2ab34","pid":"5a0d8544e986c9041df2aafe","name":"trafficking"},{"priority":3,"name_lower":"animals","_id":"5a0d86aae986c9041df2ab36","pid":"5a0d869fe986c9041df2ab34","name":"animals"},{"priority":3,"name_lower":"human","_id":"5a0d86aae986c9041df2ab37","pid":"5a0d869fe986c9041df2ab34","name":"human"},{"priority":3,"name_lower":"railways","_id":"5a0d86c9e986c9041df2ab3a","pid":"5a016637e986c90418e96ee9","name":"Railways"},{"priority":3,"name_lower":"north","_id":"5a0d86d6e986c9041df2ab3c","pid":"5a0d86c9e986c9041df2ab3a","name":"North"},{"priority":3,"name_lower":"east","_id":"5a0d86d6e986c9041df2ab3d","pid":"5a0d86c9e986c9041df2ab3a","name":"east"},{"priority":3,"name_lower":"south","_id":"5a0d86d6e986c9041df2ab3e","pid":"5a0d86c9e986c9041df2ab3a","name":"south"},{"priority":3,"name_lower":"west","_id":"5a0d86d6e986c9041df2ab3f","pid":"5a0d86c9e986c9041df2ab3a","name":"west"},{"priority":3,"name_lower":"central","_id":"5a0d86d6e986c9041df2ab40","pid":"5a0d86c9e986c9041df2ab3a","name":"central"},{"priority":3,"name_lower":"budget","_id":"5a0d86e2e986c9041df2ab46","pid":"5a0d86c9e986c9041df2ab3a","name":"budget"},{"priority":3,"name_lower":"nature","_id":"5a12c1c7e986c90418cefdfd","pid":"5a016637e986c90418e96ee9","name":"Nature"},{"priority":3,"name_lower":"animals","_id":"5a12c1d8e986c90418cefdff","pid":"5a12c1c7e986c90418cefdfd","name":"Animals"},{"priority":3,"name_lower":"humans","_id":"5a12c1d8e986c90418cefe00","pid":"5a12c1c7e986c90418cefdfd","name":"Humans"},{"priority":3,"name_lower":"amphibians animals","_id":"5a12c277e986c90418cefe03","pid":"5a12c1d8e986c90418cefdff","name":"Amphibians animals"},{"priority":3,"name_lower":"tresstrial animals","_id":"5a12c277e986c90418cefe04","pid":"5a12c1d8e986c90418cefdff","name":"Tresstrial animals"},{"priority":3,"name_lower":"aquatic animals","_id":"5a12c277e986c90418cefe05","pid":"5a12c1d8e986c90418cefdff","name":"Aquatic animals"},{"priority":3,"name_lower":"arial animals","_id":"5a12c277e986c90418cefe06","pid":"5a12c1d8e986c90418cefdff","name":"Arial animals"},{"priority":3,"name_lower":"child","_id":"5a12c2c7e986c90418cefe0f","pid":"5a12c1d8e986c90418cefe00","name":"Child"},{"priority":3,"name_lower":"baby","_id":"5a12c2c7e986c90418cefe10","pid":"5a12c1d8e986c90418cefe00","name":"Baby"},{"priority":3,"name_lower":"adult","_id":"5a12c2c7e986c90418cefe11","pid":"5a12c1d8e986c90418cefe00","name":"Adult"},{"priority":3,"name_lower":"female","_id":"5a12c2d4e986c90418cefe15","pid":"5a12c2c7e986c90418cefe11","name":"female"},{"priority":3,"name_lower":"male","_id":"5a12c2d4e986c90418cefe16","pid":"5a12c2c7e986c90418cefe11","name":"male"},{"priority":3,"name_lower":"male","_id":"5a12c2d9e986c90418cefe19","pid":"5a12c2c7e986c90418cefe10","name":"male"},{"priority":3,"name_lower":"female","_id":"5a12c2d9e986c90418cefe1a","pid":"5a12c2c7e986c90418cefe10","name":"female"},{"priority":3,"name_lower":"female","_id":"5a12c2dde986c90418cefe1d","pid":"5a12c2c7e986c90418cefe0f","name":"female"},{"priority":3,"name_lower":"male","_id":"5a12c2dde986c90418cefe1e","pid":"5a12c2c7e986c90418cefe0f","name":"male"},{"priority":3,"name_lower":"politics","_id":"5a12c2ebe986c90418cefe21","pid":"5a016637e986c90418e96ee9","name":"Politics"},{"priority":3,"name_lower":"bjp","_id":"5a12c321e986c90418cefe23","pid":"5a12c2ebe986c90418cefe21","name":"BJP"},{"priority":3,"name_lower":"aidmk","_id":"5a12c321e986c90418cefe24","pid":"5a12c2ebe986c90418cefe21","name":"AIDMK"},{"priority":3,"name_lower":"bsp","_id":"5a12c321e986c90418cefe25","pid":"5a12c2ebe986c90418cefe21","name":"BSP"},{"priority":3,"name_lower":"congress","_id":"5a12c321e986c90418cefe26","pid":"5a12c2ebe986c90418cefe21","name":"Congress"},{"priority":3,"name_lower":"aap","_id":"5a12c321e986c90418cefe27","pid":"5a12c2ebe986c90418cefe21","name":"AAP"},{"priority":3,"name_lower":"dmk","_id":"5a12c321e986c90418cefe28","pid":"5a12c2ebe986c90418cefe21","name":"DMK"},{"priority":3,"name_lower":"others","_id":"5a12c322e986c90418cefe2b","pid":"5a12c2ebe986c90418cefe21","name":"Others"},{"priority":3,"name_lower":"government","_id":"5a12c32fe986c90418cefe31","pid":"5a016637e986c90418e96ee9","name":"Government"},{"priority":3,"name_lower":"state government","_id":"5a12c33be986c90418cefe33","pid":"5a12c32fe986c90418cefe31","name":"State government"},{"priority":3,"name_lower":"central government","_id":"5a12c33be986c90418cefe34","pid":"5a12c32fe986c90418cefe31","name":"Central government"},{"priority":3,"name_lower":"president rule","_id":"5a12c361e986c90418cefe39","pid":"5a12c32fe986c90418cefe31","name":"President rule"},{"priority":3,"name_lower":"president","_id":"5a12c38fe986c90418cefe3b","pid":"5a12c33be986c90418cefe34","name":"President"},{"priority":3,"name_lower":"elections","_id":"5a12c38fe986c90418cefe3c","pid":"5a12c33be986c90418cefe34","name":"Elections"},{"priority":3,"name_lower":"prime-minister","_id":"5a12c38fe986c90418cefe3d","pid":"5a12c33be986c90418cefe34","name":"Prime-Minister"},{"priority":3,"name_lower":"parilament","_id":"5a12c38fe986c90418cefe3e","pid":"5a12c33be986c90418cefe34","name":"Parilament"},{"priority":3,"name_lower":"mps","_id":"5a12c390e986c90418cefe3f","pid":"5a12c33be986c90418cefe34","name":"MPS"},{"priority":3,"name_lower":"mla","_id":"5a12c390e986c90418cefe40","pid":"5a12c33be986c90418cefe34","name":"MLA"},{"priority":3,"name_lower":"home-minister","_id":"5a12c3d5e986c90418cefe47","pid":"5a12c33be986c90418cefe33","name":"home-minister"},{"priority":3,"name_lower":"railway-minister","_id":"5a12c3d5e986c90418cefe49","pid":"5a12c33be986c90418cefe33","name":"railway-minister"},{"priority":3,"name_lower":"mps","_id":"5a12c3d5e986c90418cefe4a","pid":"5a12c33be986c90418cefe33","name":"MPs"},{"priority":3,"name_lower":"governer","_id":"5a12c3d5e986c90418cefe4b","pid":"5a12c33be986c90418cefe33","name":"Governer"},{"priority":3,"name_lower":"elections","_id":"5a12c3d5e986c90418cefe4c","pid":"5a12c33be986c90418cefe33","name":"Elections"},{"priority":3,"name_lower":"mla","_id":"5a12c3d5e986c90418cefe4d","pid":"5a12c33be986c90418cefe33","name":"MLA"},{"priority":3,"name_lower":"cheif-minister","_id":"5a12c3d5e986c90418cefe4e","pid":"5a12c33be986c90418cefe33","name":"Cheif-Minister"}]}]



function getParentObject(parentObj){

     var allParentObject = parentObj.filter(function(o){
        return o.parent == 0;
     });

     return allParentObject;
}

function getAllParentIds(allParentObject){

    var allActualParentIds = allParentObject.map(obj =>  obj._id);

    return allActualParentIds;
}

function getAllSubChilds(subchild){

    var subChildObject = subchild.filter(function(o,i){
        return o._id == subchild[i]._id;
     });

     return subChildObject;
 }


function findNestedObjects(data,allParentObject,ActualParentId){

   var firstChild = getParentObject(data);

   var allActualParentIds = getAllParentIds(firstChild);

   var obj = {};


   //console.log('all childs', JSON.stringify(firstChild));


   if(firstChild.length){

        //allParentObject.nodes = firstChild;

        var subChildObject = getAllSubChilds(firstChild);

        console.log('sub child object ',JSON.stringify(subChildObject));

        var subChildIds = getAllParentIds(subChildObject);

        //onsole.log('all parent nodes ',JSON.stringify(allParentObject));

        //findNestedObjects(data,firstChild,ActualParentId);
   }else{

      allParentObject.nodes = [];
   }

}

var allParentObject = getParentObject(data);

var allActualParentIds = getAllParentIds(allParentObject);


for(var i=0;i<allActualParentIds.length;i++){
  findNestedObjects(data,allParentObject[i],allActualParentIds[i]);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

see fiddle with full data: https://jsfiddle.net/eabangalore/fzonzxjt/73/

please help me thanks in advance!!!

解决方案

Here's a simple recursive algorythm for building this tree (I created a simplified version of your data but everything seems to be working fine):

function setNodes(obj, data) {
	obj.nodes = data.filter(x => x.pid === obj._id);
}

function populateLevel(arr, data) {
  arr.forEach(x => {
    setNodes(x, data);
    if (x.nodes.length) {
    	populateLevel(x.nodes, data);
    }
  });
}

const json = [{
  "categories": [
  	{
      "_id": "1",
      "pid": "0",
      "name": "general-categories"
    }, {
      "priority": 3,
      "name_lower": "tragedy",
      "_id": "2",
      "pid": "1",
      "name": "Tragedy"
    }, {
      "priority": 3,
      "name_lower": "natural",
      "_id": "3",
      "pid": "1",
      "name": "natural"
    }, {
      "priority": 3,
      "name_lower": "humans",
      "_id": "4",
      "pid": "2",
      "name": "humans"
    }, {
      "priority": 3,
      "name_lower": "earthquake",
      "_id": "5",
      "pid": "2",
      "name": "earthquake"
    }, {
      "priority": 3,
      "name_lower": "floods",
      "_id": "6",
      "pid": "3",
      "name": "floods"
    }, {
      "priority": 3,
      "name_lower": "drought",
      "_id": "7",
      "pid": "3",
      "name": "drought"
    }, {
      "priority": 3,
      "name_lower": "landslides",
      "_id": "8",
      "pid": "6",
      "name": "landslides"
    }, {
      "priority": 3,
      "name_lower": "tsunami",
      "_id": "9",
      "pid": "6",
      "name": "tsunami"
    }]
}];

const data = json[0].categories;
const startingLevel = data.filter(x => x.pid === '0');

populateLevel(startingLevel, data);

console.log(startingLevel);

Here's a jsfiddle: https://jsfiddle.net/ww71otLv/

这篇关于如何从简单的json制作所需的json树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:19