简体   繁体   English

平面JSON到没有ID的层次结构/树

[英]Flat JSON to hierarchy/tree without ID

Helloo, I'm trying to convert a flat JSON to a tree structure and I'm curious if this is possible or not. 您好,我正在尝试将平面JSON转换为树结构,并且很好奇是否可行。 Any help completing my problem would be greatly appreciated. 任何帮助完成我的问题的帮助将不胜感激。

Below is what I have currently tried and am working on. 以下是我目前正在尝试并正在研究的内容。 This only successfully get the first set of children and they're children. 这样只能成功获得第一批孩子,他们就是孩子。 I haven't been able to make it passed this point. 我一直无法通过这一点。 I fear this may not be possible. 我担心这可能无法实现。

I know the root before I started so I think it should be possible. 我在开始之前就知道根源,所以我认为应该有可能。 For this case the root is "Provide Automated Military Health Systems Functions". 对于这种情况,其根源为“提供自动化的军事卫生系统功能”。

The tree JSON would end up looking like: 树的JSON最终看起来像:

{ "name": "Provide Automated Military Health Systems Functions", "children": [ { "name": "Provide Infrastructure Support Functions", "children": [ "name": "Provide Data Management Functions" "children": [etc, etc] ] }, { "name": "Provide Clinical Support Functions", "children": [etc etc] }, { "name": "Provide Non-Clinical Support Functions", "children": [etc etc] } ] }

Here is the fiddle I've been working off of also: http://jsfiddle.net/ydgbkv39/ 这也是我一直在努力的小提琴: http : //jsfiddle.net/ydgbkv39/

The fiddle does a console print of the first 2 levels but I can't seem to make it past that point. 小提琴在前两个级别进行控制台打印,但我似乎无法超越这一点。

Hopefully, someone can help me out! 希望有人可以帮助我!

 var data = [ { "Upstream": "Provide Automated Military Health Systems Functions", "Downstream": "Provide Infrastructure Support Functions" }, { "Upstream": "Provide Automated Military Health Systems Functions", "Downstream": "Provide Clinical Support Functions" }, { "Upstream": "Provide Automated Military Health Systems Functions", "Downstream": "Provide Non-Clinical Support Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide Data Management Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide Maintenance Utilities Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide Business Rules Execution Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide MHS Health Portal Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide Security Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Care Management Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Nutrition Information Management Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Healthcare Specialty Services Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Lab Support Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Pharmacy Support Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Blood Management Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Medical Imagery Support Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Operations Support Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Order Results Care Management Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Medical Orders Maintenance Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Episodes of Care Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Executive Decision Support Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Manage Family Support Process Workflow (BEA)" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Health Records Support Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Resource Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Medical Readiness Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Population Health Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Medical Logistics Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Patient Directory Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Provider Information Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Patient Administration Functions" } ]; Array.prototype.diff = function(a) { return this.filter(function(i) {return a.indexOf(i) < 0;}); }; var upstreamArr = []; var downstreamArr = []; data.forEach(function (a) { upstreamArr.push(a.Upstream); downstreamArr.push(a.Downstream); }, {}); var root = upstreamArr.diff(downstreamArr); root = root[0]; var tree = {}; tree.name = root; tree.children = []; data.forEach(function (a) { if(a.Upstream === root) { if(tree.children.indexOf(a.Downstream) === -1) { tree.children.push(a.Downstream); } } }, {}); function buildTree(d) { if(d.children.length > 0) { for(var i = 0; i < d.children.length; i++) { findKids(d, d.children[i]); } } return d; } function findKids(d, child) { let obj = {}; obj.children = []; data.forEach(function (a) { if(a.Upstream === child) { obj.name = child; if(obj.children.indexOf(a.Downstream) === -1) { obj.children.push(a.Downstream); } } }, {}); var ind = d.children.indexOf(child); return d.children[ind] = obj; } /*function eachRecursive(obj) { for (var k in obj) { if (typeof obj[k] == "object" && obj[k] !== null) { eachRecursive(obj[k]); } else { } } }*/ console.log(buildTree(tree)); 

You will need to use an object to map the key (in this case the 'name') to the value (in this case the tree node). 您将需要使用一个对象将键(在本例中为“名称”)映射到值(在本例中为树节点)。

I named my functions so you can see how it works below: 我为函数命名,以便您可以在下面看到它的工作方式:

 var data = [ { "Upstream": "Provide Automated Military Health Systems Functions", "Downstream": "Provide Infrastructure Support Functions" }, { "Upstream": "Provide Automated Military Health Systems Functions", "Downstream": "Provide Clinical Support Functions" }, { "Upstream": "Provide Automated Military Health Systems Functions", "Downstream": "Provide Non-Clinical Support Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide Data Management Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide Maintenance Utilities Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide Business Rules Execution Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide MHS Health Portal Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide Security Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Care Management Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Nutrition Information Management Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Healthcare Specialty Services Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Lab Support Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Pharmacy Support Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Blood Management Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Medical Imagery Support Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Operations Support Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Order Results Care Management Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Medical Orders Maintenance Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Episodes of Care Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Executive Decision Support Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Manage Family Support Process Workflow (BEA)" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Health Records Support Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Resource Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Medical Readiness Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Population Health Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Medical Logistics Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Patient Directory Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Provider Information Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Patient Administration Functions" } ]; var nameToNode = {}; function getNodeByName(name) { if (!nameToNode[name]) { nameToNode[name] = { name: name, children: [] }; } return nameToNode[name]; } function getRootNode() { for (var name in nameToNode) { if (nameToNode[name].children.length > 0) { return nameToNode[name]; } } } function buildTree() { data.forEach(o => { var up = getNodeByName(o.Upstream); var down = getNodeByName(o.Downstream); up.children.push(down); }); } buildTree(); var root = getRootNode(); console.log(root); 

The getRootNode function assumes all of the original records will form a single Tree. getRootNode函数假定所有原始记录将形成一个树。 If you are expecting a forest with multiple root nodes, then you will need to change the logic a bit. 如果您期望一个具有多个根节点的林,则需要稍微更改逻辑。

You can keep track of all the nodes by storing them in a "flat" object (where each items's name is its key in the flat object). 您可以通过将所有节点存储在“平面”对象中来跟踪所有节点(每个项目的名称是其在平面对象中的键)。 If the name doesn't exist in the flat object yet, you create it; 如果名称在平面对象中尚不存在,请创建它;否则,请创建它。 otherwise, you use its reference to add children. 否则,您将使用其引用来添加子代。

 var data = [ { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide Data Management Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide Maintenance Utilities Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide Business Rules Execution Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide MHS Health Portal Functions" }, { "Upstream": "Provide Infrastructure Support Functions", "Downstream": "Provide Security Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Care Management Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Nutrition Information Management Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Healthcare Specialty Services Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Lab Support Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Pharmacy Support Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Blood Management Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Medical Imagery Support Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Operations Support Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Order Results Care Management Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Medical Orders Maintenance Functions" }, { "Upstream": "Provide Clinical Support Functions", "Downstream": "Provide Episodes of Care Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Executive Decision Support Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Manage Family Support Process Workflow (BEA)" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Health Records Support Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Resource Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Medical Readiness Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Population Health Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Medical Logistics Management Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Patient Directory Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Provider Information Functions" }, { "Upstream": "Provide Non-Clinical Support Functions", "Downstream": "Provide Patient Administration Functions" }, { "Upstream": "Provide Automated Military Health Systems Functions", "Downstream": "Provide Infrastructure Support Functions" }, { "Upstream": "Provide Automated Military Health Systems Functions", "Downstream": "Provide Clinical Support Functions" }, { "Upstream": "Provide Automated Military Health Systems Functions", "Downstream": "Provide Non-Clinical Support Functions" } ]; var tree = {}, flat = {}; var setRoots = function(data){ var unique = {}; data.forEach(function(item){ unique[item.Upstream] = true; }); data.forEach(function(item){ if(unique[item.Downstream]){ delete unique[item.Downstream]; } }); var keys = Object.keys(unique), rootName = data[0].Upstream; if(keys.length == 1){ rootName = keys[0]; } tree = {name: rootName}; flat[rootName] = tree; } var addItemToTree = function(item){ var parent = flat[item.Upstream], child = flat[item.Downstream]; if(!parent){ parent = flat[item.Upstream] = { name: item.Upstream } } if(!child){ child = flat[item.Downstream] = { name: item.Downstream }; } if(!parent.children){ parent.children = []; } parent.children.push(child); } setRoots(data); data.forEach(addItemToTree); console.log(tree); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM