javascript - 将 JSON 扁平化为没有 ID 的层次结构/树

标签 javascript json parent-child hierarchy

你好,我正在尝试将平面 JSON 转换为树结构,我很好奇这是否可能。任何帮助解决我的问题将不胜感激。

以下是我目前尝试过并正在研究的内容。这样才成功得到了第一组 child ,而且他们还是 child 。我还没能渡过这一关。我担心这可能是不可能的。

我在开始之前就知道根源,所以我认为这应该是可能的。对于本例,根是“提供自动化军事卫生系统功能”。

树 JSON 最终看起来像:

{ "name": "提供自动化军事卫生系统功能", “ children ”: [ { "name": "提供基础设施支撑功能", “ children ”: [ "name": "提供数据管理功能" “ children ”:[等等,等等] ] }, { "name": "提供临床支持功能", “ children ”:[等等] }, { "name": "提供非临床支持功能", “ children ”:[等等] } ] }

这也是我一直在研究的 fiddle :http://jsfiddle.net/ydgbkv39/

fiddle 会打印前两个级别的控制台,但我似乎无法超越这一点。

希望有人能帮助我!

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 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 函数假设所有原始记录将形成一个树。如果您期望森林具有多个根节点,那么您将需要稍微更改逻辑。

关于javascript - 将 JSON 扁平化为没有 ID 的层次结构/树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47058524/

相关文章:

html - 让 div 保持在父级之上

php - 调用的子常量在父级的静态函数中不可用

mysql - 获取包含子项和子项详细信息计数的父项的 MySQL 结果

javascript - javascript 中匹配 { 和 } 之间字符串的正则表达式?

java - 在 Java 中转义 JSON 字符串

javascript - ColdFusion 的 SerializeJSON 中的日语字符的编码问题

javascript - 来自 JSON 数组的准确值

javascript - 将字符串替换为javascript正则表达式中的括号

javascript - 为什么 ng-include 不工作?

javascript - 改变JCrop的aspectRatio的方法是什么?