将多个路径转换为子Javascript数组

时间:2016-11-22 20:37:16

标签: javascript reactjs

我正在尝试将具有多个路径的多个数组转换为一个平面的子数组。这样做的最佳方式是什么?我尝试使用对象引用创建它,但它创建了一个无限循环。我还有什么方法可以做到这一点?

感谢您的帮助

这是输入(因为有多个路径需要组合,所以还有多个节点是相同的。例如A到B发生两次):

arry = [
 [
  {"title":"abc", "from":"", "current": "A", "count": 1}, 
  {"title":"abc", "from":"A", "current": "B", "count": 3}, 
  {"title":"abc", "from":"B", "current": "C", "count": 4}, 
  {"title":"abc", "from":"C", "current": "D", "count": 1}, 
 ],
 [
  {"title":"abc", "from":"", "current": "A", "count": 1}, 
  {"title":"abc", "from":"A", "current": "B", "count": 1}, 
  {"title":"abc", "from":"B", "current": "D", "count": 1}, 
 ],
 [
  {"title":"abc", "from":"", "current": "A", "count": 1}, 
  {"title":"abc", "from":"A", "current": "J", "count": 1}, 
  {"title":"abc", "from":"J", "current": "C", "count": 2}, 
  {"title":"abc", "from":"C", "current": "D", "count": 3}, 
 ],
 [
  {"title":"abc", "from":"", "current": "A", "count": 1}, 
  {"title":"abc", "from":"A", "current": "B", "count": 3}, 
  {"title":"abc", "from":"B", "current": "F", "count": 1}, 
  {"title":"abc", "from":"F", "current": "D", "count": 1}, 
 ],
 //THIS COULD BE INFINITE
]//for testing

期望的输出

{
"title": "A",
"count": 1,
"children": [
{
  "value": "B",
   "count": 4, //The count is sum of the nodes A->B = 3 and C->B 1
  "children": [
      {
      "value": "C",
       "count": 6,
      "children": [...]
     },
     {
      "value": "D" ,
      "count": 6,
     "children": [...]
    },
     {
      "value": "F" ,
      "count": 6,
     "children": [...]
    }
  ],
  {
      "value": "C" ,
      "count": 6,
     "children": [...]
    },
    {
      "value": "J" ,
      "count": 6,
     "children": [...]
    },
 ]
..
}

这是我到目前为止所做的:

getTreeData(){
var map = {}, node, roots = [];
for (var i = data.nodes.length - 1; i >= 0; i--) {
  for (var j = data.nodes[i].length - 1; j >=0 ; j --) {
      node = data.nodes[i][j];
      node.children = [];
      if(!(data.nodes[i][j].current in map)){
        map[data.nodes[i][j].current] = node; // use map to look-up the parents  
      }else{
        node = map[data.nodes[i][j].current]
      }
      if (node.from != null && node.from !== "") {
          // if(map[node.from] != null && !(data.nodes[i][j].current in map[data.nodes[i][j].from].children)){
            // console.log()
          if(map[node.from] != null && ! map[data.nodes[i][j].from].children.some(function (el) { return el.current === data.nodes[i][j].current; })){
            map[data.nodes[i][j].from].children.push(node);
            // map[data.nodes[i][j].from].children[data.nodes[i][j].current] = node;
          }
      } else if(roots.length <= 0){
          roots.push(node);
      }
  }
}
return roots;
}

1 个答案:

答案 0 :(得分:0)

创建树:

var arr = [
  {"title":"abc", "from":"", "current": "A", "count": 1}, 
  {"title":"abc", "from":"A", "current": "B", "count": 3}, 
  {"title":"abc", "from":"B", "current": "C", "count": 4}, 
  {"title":"abc", "from":"C", "current": "D", "count": 1}, 
 ];

arr.reduceRight((a, b) => {
    b.child = a;
    return b;
 });
相关问题