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

猜你喜欢

转载自www.cnblogs.com/aipeli/p/12235498.html