JSON数据格式生成无限级树结构

 

buildTree(data, id, pid) {
    let map = {};
    data.forEach(function (item) {
      // map[item.id] = item; 
      eval('map[item.' + id + '] = item;')   //当前ID
    });
    let val = [];
    data.forEach(function (item) {
      // var parent = map[item.parent_group_id];      
      let parent = eval('map[item.' + pid + '];')     //上级ID
      if (parent) {
        (parent.children || (parent.children = [])).push(item);
      } else {
        val.push(item);
      }
    });
    return val;
  }

 

posted @ 2020-01-27 10:39  泡面 @ 幸福  阅读(465)  评论(0编辑  收藏  举报