没有ID

时间:2017-11-01 15:40:38

标签: javascript json parent-child hierarchy

Helloo,我试图将平面JSON转换为树形结构,如果可能或不可能,我很好奇。任何完成我的问题的帮助将不胜感激。

以下是我目前正在尝试和正在进行的工作。这只能成功地获得第一批孩子和他们的孩子。我还没能通过这一点。我担心这可能是不可能的。

在我开始之前我知道根,所以我认为它应该是可能的。对于这种情况,根是"提供自动军事健康系统功能"。

树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] } ] }

这也是我一直在努力的小提琴:http://jsfiddle.net/ydgbkv39/

这个小提琴做了前两个级别的控制台打印,但我似乎无法超越这一点。

希望有人可以帮助我!



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));
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

您需要使用一个对象将键(在本例中为&#39; name&#39;)映射到值(在本例中为树节点)。

我命名了我的功能,以便您可以在下面看到它的工作原理:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

getRootNode函数假定所有原始记录将构成一个树。如果您期望具有多个根节点的林,则需要稍微更改一下逻辑。

答案 1 :(得分:1)

您可以通过将所有节点存储在&#34; flat&#34;中来跟踪它们。对象(其中每个项的名称是其在平面对象中的键)。如果名称在平面对象中不存在,则创建它;否则,您使用其引用添加子项。

&#13;
&#13;
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);
&#13;
&#13;
&#13;