JSON到Tree JS

时间:2017-02-09 10:57:47

标签: javascript json tree

有人可以帮我改变这个json:

这些是规则:     JSON(A)的节点按特定顺序排序     JSON(A)的节点是可变的     "下属"当没有附加子节点时,节点不能存在(参见markcorderoi或richard)     状态表示就业状态,1 =工作,2 =结束。连接到已结束(2)节点的所有工作节点应使用相邻(上行链路)工作(1)节点(即连接到nssi的marcoderoi,删除最终树中的rudy)     JSON(B)不应包含已结束的节点

JSON A:

[
  {
    "manager_name": "nssi",
    "login_name": "nishanthi",
    "status": 2
  }, 
  {
      "manager_name": "mbarcelona",
      "login_name": "nssi",
      "status": 1
  }, 
  {
      "manager_name": "nishanthi",
      "login_name": "markcorderoi",
      "status": 1
  }, 
  {
      "manager_name": "mbarcelona",
      "login_name": "richard",
      "status": 1
  }, 
  {
      "manager_name": "letecia",
      "login_name": "kamran",
      "status": 1
  }, 
  {
      "manager_name": "letecia",
      "login_name": "rudy",
      "status": 2
  }, 
  {
      "manager_name": "rudy",
      "login_name": "sol",
      "status": 2
  }, 
  {
    "manager_name": "gloria",
    "login_name": "maria",
    "status": 1
  }, 
  {
    "manager_name": "markcorderoi",
    "login_name": "gloria",
    "status": 1
  }
]

到此?

JSON B:

[  
   {  
      "subordinate":[  
         {  
            "subordinate":[  
               {  
                  "subordinate":[  
                     {  
                        "subordinate":[  
                           {  
                              "name":"maria"
                           }
                        ],
                        "name":"gloria"
                     }
                  ],
                  "name":"markcorderoi"
               }
            ],
            "name":"nssi"
         },
         {  
            "name":"richard"
         }
      ],
      "name":"mbarcelona"
   },
   {  
      "subordinate":[  
         {  
            "name":"kamran"
         },
         {  
            "name":"sol"
         }
      ],
      "name":"letecia"
   }
]

我真的很难只使用Javascript做一些算法。

1 个答案:

答案 0 :(得分:1)

这有点棘手,因为您的数据不包含没有父项的项,因此会收集所有子项以及所有父节点。只有父母是结果集中没有孩子的用途。

ended中,如果节点作为父节点工作,则会收集并稍后删除所有status === 2个节点。

function getTree(data) {
    var o = {},
        children = {},
        parents = {},
        ended = [];

    data.forEach(function (a) {
        var temp = { name: a.login_name };

        children[a.login_name] = true;
        parents[a.manager_name] = true;
        if (a.status === 2) {
            ended.push(a);
        }
        if (o[a.login_name] && o[a.login_name].subordinate) {
            temp.subordinate = o[a.login_name].subordinate;
        }
        o[a.login_name] = temp;
        o[a.manager_name] = o[a.manager_name] || { name: a.manager_name };
        o[a.manager_name].subordinate = o[a.manager_name].subordinate || [];
        o[a.manager_name].subordinate.push(temp);
    });

    ended.forEach(function (a) {
        var index = -1;
        if (o[a.login_name].subordinate) {
            o[a.manager_name].subordinate.some(function (b, i) {
                if (b === o[a.login_name]) {
                    index = i;
                    return true;
                }
            });
            if (index !== -1) {
                o[a.manager_name].subordinate.splice(index, 1);
                o[a.manager_name].subordinate = o[a.manager_name].subordinate.concat(o[a.login_name].subordinate);
            }
        }
    });

    Object.keys(children).forEach(function (k) {
        delete parents[k];
    });

    return Object.keys(parents).map(function (k) {
        return o[k];
    });
}

var data = [{ manager_name: "nssi", login_name: "nishanthi", status: 2 }, { manager_name: "mbarcelona", login_name: "nssi", status: 1 }, { manager_name: "nishanthi", login_name: "markcorderoi", status: 1 }, { manager_name: "mbarcelona", login_name: "richard", status: 1 }, { manager_name: "letecia", login_name: "kamran", status: 1 }, { manager_name: "letecia", login_name: "rudy", status: 2 }, { manager_name: "rudy", login_name: "sol", status: 2 }, { manager_name: "gloria", login_name: "maria", status: 1 }, { manager_name: "markcorderoi", login_name: "gloria", status: 1 }];

console.log(getTree(data));
.as-console-wrapper { max-height: 100% !important; top: 0; }