美文网首页
将后端返回的 List 转换成树结构

将后端返回的 List 转换成树结构

作者: 赛亚人之神 | 来源:发表于2018-04-16 10:48 被阅读15次
this.$http.post('/api/staff/all').then(res => {

        if (200 === res.data.status) {
          let data = res.data.content;

          let map = {};

          // 将数组转换成 map
          data.forEach(item => map[item.organizationCode] = item);

          console.log(map);

          let output = [];

          data.forEach(item => {
            // 根据父 id 在 map 中查询是否存在,如果找到说明是该父元素的 child, 否则则是顶级元素
            let parent = map[item.origanizationParentCode];
            if (parent) {
              (parent.children || (parent.children = [])).push(item);
            } else {
              output.push(item);
            }
          });

          console.log(output, '过滤结果');

        }
      })

相关文章

网友评论

      本文标题:将后端返回的 List 转换成树结构

      本文链接:https://www.haomeiwen.com/subject/csvskftx.html