文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

JS前端二维数组生成树形结构示例详解

2024-04-02 19:55

关注

问题描述

前端在构建国家的省市区结构时,接口返回的不是树形结构,这个时候就需要我们进行转化。以下数据为例

 [
    [
        {
            "districtId": 1586533852834,
            "parentCode": "000",
            "nodeCode": "000001",
            "name": "浙江省",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533872922,
            "parentCode": "000001",
            "nodeCode": "000001001",
            "name": "杭州市",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533872940,
            "parentCode": "000001001",
            "nodeCode": "000001001001",
            "name": "上城区",
            "districtType": "HUADONG",
            "districtTypeName": null
        }
    ],
    [
        {
            "districtId": 1586533852834,
            "parentCode": "000",
            "nodeCode": "000001",
            "name": "浙江省",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533872922,
            "parentCode": "000001",
            "nodeCode": "000001001",
            "name": "杭州市",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533872961,
            "parentCode": "000001001",
            "nodeCode": "000001001002",
            "name": "下城区",
            "districtType": "HUADONG",
            "districtTypeName": null
        }
    ],
    [
        {
            "districtId": 1586533852834,
            "parentCode": "000",
            "nodeCode": "000001",
            "name": "浙江省",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533872922,
            "parentCode": "000001",
            "nodeCode": "000001001",
            "name": "杭州市",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533872980,
            "parentCode": "000001001",
            "nodeCode": "000001001003",
            "name": "临安区",
            "districtType": "HUADONG",
            "districtTypeName": null
        }
    ],
    [
        {
            "districtId": 1586533852834,
            "parentCode": "000",
            "nodeCode": "000001",
            "name": "浙江省",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533873196,
            "parentCode": "000001",
            "nodeCode": "000001002",
            "name": "宁波市",
            "districtType": "HUADONG",
            "districtTypeName": null
        }
    ],
    [
        {
            "districtId": 1586533852834,
            "parentCode": "000",
            "nodeCode": "000001",
            "name": "浙江省",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533873432,
            "parentCode": "000001",
            "nodeCode": "000001003",
            "name": "温州市",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533873468,
            "parentCode": "000001003",
            "nodeCode": "000001003002",
            "name": "平阳县",
            "districtType": "HUADONG",
            "districtTypeName": null
        }
    ],
    [
        {
            "districtId": 1586533852834,
            "parentCode": "000",
            "nodeCode": "000001",
            "name": "浙江省",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533873432,
            "parentCode": "000001",
            "nodeCode": "000001003",
            "name": "温州市",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533873486,
            "parentCode": "000001003",
            "nodeCode": "000001003003",
            "name": "文成县",
            "districtType": "HUADONG",
            "districtTypeName": null
        }
    ]
]
[
    {
        "label": "浙江省",
        "value": 1586533852834,
        "parentCode": "000",
        "nodeCode": "000001",
        "children": [
            {
                "label": "杭州市",
                "value": 1586533872922,
                "parentCode": "000001",
                "nodeCode": "000001001",
                "children": [
                    {
                        "label": "上城区",
                        "value": 1586533872940,
                        "parentCode": "000001001",
                        "nodeCode": "000001001001"
                    },
                    {
                        "label": "下城区",
                        "value": 1586533872961,
                        "parentCode": "000001001",
                        "nodeCode": "000001001002"
                    },
                    {
                        "label": "临安区",
                        "value": 1586533872980,
                        "parentCode": "000001001",
                        "nodeCode": "000001001003"
                    }
                ]
            },
            {
                "label": "宁波市",
                "value": 1586533873196,
                "parentCode": "000001",
                "nodeCode": "000001002"
            },
            {
                "label": "温州市",
                "value": 1586533873432,
                "parentCode": "000001",
                "nodeCode": "000001003",
                "children": [
                    {
                        "label": "平阳县",
                        "value": 1586533873468,
                        "parentCode": "000001003",
                        "nodeCode": "000001003002"
                    },
                    {
                        "label": "文成县",
                        "value": 1586533873486,
                        "parentCode": "000001003",
                        "nodeCode": "000001003003"
                    }
                ]
            }
        ]
    }
]

实现步骤

① 观察输入的数据结构为二维数组,每个数组中存储了省市区的全部数据,此时首先需要将二维数组一维化,以取得所有的节点数据,用于后续构建树形结构。

let arr = [].concat.apply([], data)

② 得到所有需要构建树形结构的数据后,发现数组中存在重复数据

arrayToTree(data, rootNode) {
  let temp = {}
  let reduceArr = data.reduce((current, next) => {
    temp[next.districtId] ? "" : temp[next.districtId] = current.push(next)
    return current
  },[])
}

③ 数据规范化处理,把所有的数据处理成需要的节点数据

arrayToTree(data, rootNode) {
  let temp = {}
  let reduceArr = data.reduce((current, next) => {
    temp[next.districtId] ? "" : temp[next.districtId] = current.push(next)
    return current
  },[])
  // 2.数组规范化
  let result = []
  reduceArr.forEach(item => {
    result.push({
      label:item.name,
      value:item.districtId,
      parentCode:item.parentCode,
      nodeCode:item.nodeCode,
      children: []
    })
  })
}

④ 采用递归的方式构建树形结构

getTreeList(data, rootNode, list,{label, value,parentCode, nodeCode, children}) {
  // 构建父节点
  data.forEach(item =>{
    if (item.parentCode === rootNode) {
      list.push(item)
    }
  })

  list.forEach(item => {
    item.children = []
    // 构建子节点
    this.getTreeList(data, item.nodeCode, item.children,{label, value,parentCode, nodeCode, children});
    // 删除空叶子节点
    if (item.children.length === 0) {
      delete item.children;
    }
  })
  return list;
}

完整代码

arrayToTree(data, rootNode, {label = 'label', value = 'value',parentCode = 'parentCode', nodeCode = 'nodeCode', children = ''} = {}) {
      // 1.数组去重
      let temp = {}
      let reduceArr = data.reduce((current, next) => {
        temp[next.districtId] ? "" : temp[next.districtId] = current.push(next)
        return current
      },[])
      // 2.数组规范化
      let result = []
      reduceArr.forEach(item => {
        result.push({
          label:item.name,
          value:item.districtId,
          parentCode:item.parentCode,
          nodeCode:item.nodeCode,
          children: []
        })
      })
      // 3.构建树形结构
      return this.getTreeList(result,rootNode,[],{
        label,value,parentCode,nodeCode,children
      });
    },
    // 递归构建树形结构
    getTreeList(data, rootNode, list,{label, value,parentCode, nodeCode, children}) {
      data.forEach(item =>{
        if (item.parentCode === rootNode) {
          list.push(item)
        }
      })

      list.forEach(item => {
        item.children = []
        this.getTreeList(data, item.nodeCode, item.children,{label, value,parentCode, nodeCode, children});
        if (item.children.length === 0) {
          delete item.children;
        }
      })
      return list;
    },

以上就是JS前端二维数组生成树形结构示例详解的详细内容,更多关于JS二维数组树形结构的资料请关注编程网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-前端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯