我在将简单json
转换为json
树
我需要遵循的步骤是:
找到有pid:0
的父母会有一些_id:'5a016637e986c90418e96ee9'
在其他_id:5a016637e986c90418e96ee9
中找到pid
以上为object
例如:会导致_id:5a0d852ce986c9041df2aafb
和其他一些}
_id
i,e 5a0d852ce986c9041df2aafb
等...... 这是我的json: https://jsfiddle.net/eabangalore/26gxm6dz/5/
我想将我的json转换为如下所示:
[
{
"_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 ............
}
]
}
]
}
]
}
]
在这里看到输出json这个我想如何转换我的json: https://jsfiddle.net/Resendra/0uzmoy34/
查看完整数据: https://jsfiddle.net/eabangalore/fzonzxjt/73/
这是我尝试过的(有一半样本数据):
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]);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
查看完整数据: https://jsfiddle.net/eabangalore/fzonzxjt/73/
请提前帮助我!
答案 0 :(得分:3)
这是一个用于构建此树的简单递归算法(我创建了数据的简化版本,但一切似乎都正常工作):
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);
这是一个jsfiddle:https://jsfiddle.net/ww71otLv/
答案 1 :(得分:1)
我不得不减少你的数据集,但这应该做你希望的一切。
首先,我们找到具有id的节点,然后找到该节点的所有子节点,然后找到该节点的所有父节点。所有这些节点都被添加到扁平数组中,然后变为树格式。
从这里的帖子修改树逻辑:Build tree array from flat array in javascript
var dataObj = [{
"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": "rape",
"_id": "5a0d863ce986c9041df2ab14",
"pid": "5a0d8544e986c9041df2aafe",
"name": "rape"
}, {
"priority": 3,
"name_lower": "kidnap",
"_id": "5a0d863ce986c9041df2ab15",
"pid": "5a0d8544e986c9041df2aafe",
"name": "kidnap"
}, {
"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": "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"
}]
}];
// Find the all nodes associated to an id
function getNodesById(idToFind, nodesToSearch) {
var nodeFound = nodesToSearch.find(possibleNode => possibleNode._id == idToFind);
var relatedNodes = [ nodeFound ];
if (nodeFound.pid != 0) {
relatedNodes = relatedNodes.concat(getParentNodes(nodeFound.pid, nodesToSearch));
}
relatedNodes = relatedNodes.concat(getChildrenNodes(nodeFound._id, nodesToSearch));
return relatedNodes;
}
// Get all parent nodes for a particular id, recursively
function getParentNodes(idToFind, nodesToSearch) {
var nodeFound = nodesToSearch.find(possibleNode => possibleNode._id == idToFind);
var relatedNodes = [ nodeFound ];
if (nodeFound.pid != 0) {
relatedNodes = relatedNodes.concat(getParentNodes(nodeFound.pid));
}
return relatedNodes;
}
// Get all child nodes for a particular node, recursively
function getChildrenNodes(idToFind, nodesToSearch) {
var relatedNodes = nodesToSearch.filter(possibleNode => possibleNode.pid == idToFind);
if (relatedNodes.length > 0) {
relatedNodes.forEach(childNode => relatedNodes = relatedNodes.concat(getChildrenNodes(childNode._id, nodesToSearch)));
}
return relatedNodes;
}
// Loop flat array and build tree modified from https://stackoverflow.com/questions/18017869/build-tree-array-from-flat-array-in-javascript
function list_to_tree(list) {
var map = {}, node, roots = [], i;
for (i = 0; i < list.length; i += 1) {
map[list[i]._id] = i; // initialize the map
list[i].nodes = []; // initialize the children
}
for (i = 0; i < list.length; i += 1) {
node = list[i];
if (node.pid !== "0") {
// if you have dangling branches check that map[node.pid] exists
list[map[node.pid]].nodes.push(node);
} else {
roots.push(node);
}
}
return roots;
}
// Nodes to search, could easily be a loop
var categoryNodes = dataObj[0].categories;
// Filtered flat array of nodes associated to an id
var filteredNodes = getNodesById('5a0d852ce986c9041df2aafb', categoryNodes);
// Make the flat array a tree structured array
console.log(list_to_tree(filteredNodes));
&#13;
我确信有更好的方法可以做到这一点,但这应该符合您的需求。