平 JSON 到 hierarchy/tree 无 ID
Flat JSON to hierarchy/tree without ID
你好,我正在尝试将平面 JSON 转换为树结构,我很好奇这是否可能。任何帮助解决我的问题的帮助将不胜感激。
以下是我目前已经尝试和正在进行的工作。这才成功得到第一组children,他们是children。我还没能让它通过这一点。恐怕这不可能。
我在开始之前就知道了根源,所以我认为这应该是可能的。对于这种情况,根是 "Provide Automated Military Health Systems Functions".
树 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]
}
]
}
这是我一直在研究的 fiddle:http://jsfiddle.net/ydgbkv39/
fiddle 打印了前 2 个级别的控制台,但我似乎无法通过该点。
希望有人能帮帮我!
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));
您需要使用对象将键(在本例中为 'name')映射到值(在本例中为树节点)。
我命名了我的函数,这样你就可以在下面看到它是如何工作的:
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);
getRootNode
函数假定所有原始记录将形成一个树。如果您希望森林具有多个根节点,那么您需要稍微更改一下逻辑。
您可以通过将它们存储在 "flat" object 中来跟踪所有节点(其中每个项目的名称是其在平面 object 中的键)。如果该名称在公寓 object 中尚不存在,则创建它;否则,您使用其引用添加 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);
你好,我正在尝试将平面 JSON 转换为树结构,我很好奇这是否可能。任何帮助解决我的问题的帮助将不胜感激。
以下是我目前已经尝试和正在进行的工作。这才成功得到第一组children,他们是children。我还没能让它通过这一点。恐怕这不可能。
我在开始之前就知道了根源,所以我认为这应该是可能的。对于这种情况,根是 "Provide Automated Military Health Systems Functions".
树 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]
}
]
}
这是我一直在研究的 fiddle:http://jsfiddle.net/ydgbkv39/
fiddle 打印了前 2 个级别的控制台,但我似乎无法通过该点。
希望有人能帮帮我!
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));
您需要使用对象将键(在本例中为 'name')映射到值(在本例中为树节点)。
我命名了我的函数,这样你就可以在下面看到它是如何工作的:
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);
getRootNode
函数假定所有原始记录将形成一个树。如果您希望森林具有多个根节点,那么您需要稍微更改一下逻辑。
您可以通过将它们存储在 "flat" object 中来跟踪所有节点(其中每个项目的名称是其在平面 object 中的键)。如果该名称在公寓 object 中尚不存在,则创建它;否则,您使用其引用添加 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);