返回顶部
首页 > 资讯 > 前端开发 > JavaScript >JS前端二维数组生成树形结构示例详解
  • 298
分享到

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

2024-04-02 19:04:59 298人浏览 安东尼
摘要

目录问题描述实现步骤完整代码问题描述 前端在构建国家的省市区结构时,接口返回的不是树形结构,这个时候就需要我们进行转化。以下数据为例 [ [ {

问题描述

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

 [
    [
        {
            "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二维数组树形结构的资料请关注编程网其它相关文章!

--结束END--

本文标题: JS前端二维数组生成树形结构示例详解

本文链接: https://lsjlt.com/news/167625.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作