将JSON平坦化为没有ID的层次结构/树

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));

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

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);

您将需要使用一个对象将键(在本例中为'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);
链接地址: http://www.djcxy.com/p/19049.html

上一篇: Flat JSON to hierarchy/tree without ID

下一篇: Unique counts in JavaScript array, sorted by counts