前端纯数组转树形结构

问题描述

前端需要处理后端返回的数据,展示如下。

解决方式

因为使用ProTable组件,那么数据只要携带children字段,就可以如上图展示。

方式一:后端返回数据的时候,直接封装好,如下:

rust 复制代码
const testMenuList = [
  {
    "id": 100100,
    "parentId": 0,
    "icon": "setting",
    "name": "系统管理",
    "path": "",
    "uri": "",
    "sort": 0,
    "hidden": 1,
    "type": 0,
    "children": [
		{
		    "id": 100101,
		    "parentId": 100100,
		    "icon": "",
		    "name": "app版本管理",
		    "path": "",
		    "uri": "",
		    "sort": 0,
		    "hidden": 1,
		    "type": 1,
		},
		{
			"id": 100102,
		    "parentId": 100100,
		    "icon": "",
		    "name": "部门管理",
		    "path": "",
		    "uri": "",
		    "sort": 1,
		    "hidden": 1,
		    "type": 1,
		},
		{
		    "id": 100103,
		    "parentId": 100100,
		    "icon": "",
		    "name": "角色管理",
		    "path": "",
		    "uri": "",
		    "sort": 2,
		    "hidden": 1,
		    "type": 1,
		},
		{
		    "id": 100104,
		    "parentId": 100100,
		    "icon": "",
		    "name": "菜单管理",
		    "path": "",
		    "uri": "",
		    "sort": 3,
		    "hidden": 1,
		    "type": 1,
		},
	]
  },
  {
    "id": 100200,
    "parentId": 0,
    "icon": "message",
    "name": "新闻管理",
    "path": "",
    "uri": "",
    "sort": 1,
    "hidden": 1,
    "type": 0,
    "children":[
		{
		    "id": 100201,
		    "parentId": 100200,
		    "icon": "",
		    "name": "新闻首页维护",
		    "path": "",
		    "uri": "",
		    "sort": 0,
		    "hidden": 1,
		    "type": 1,
		},
		{
		    "id": 100202,
		    "parentId": 100200,
		    "icon": "",
		    "name": "置顶新闻",
		    "path": "",
		    "uri": "",
		    "sort": 1,
		    "hidden": 1,
		    "type": 1,
		},
		{
		    "id": 100203,
		    "parentId": 100200,
		    "icon": "",
		    "name": "专题新闻",
		    "path": "",
		    "uri": "",
		    "sort": 2,
		    "hidden": 1,
		    "type": 1,
		},
		{
		    "id": 100204,
		    "parentId": 100200,
		    "icon": "",
		    "name": "地方性新闻",
		    "path": "",
		    "uri": "",
		    "sort": 3,
		    "hidden": 1,
		    "type": 1,
		},
	]
  },
]

方式二:

后端不携带children字段,返回所有数据由前端进行处理,如下格式:

haskell 复制代码
const testMenuList = [
  {
    "id": 100100,
    "parentId": 0,
    "icon": "setting",
    "name": "系统管理",
    "path": "",
    "uri": "",
    "sort": 0,
    "hidden": 1,
    "type": 0,
  },
  {
    "id": 100101,
    "parentId": 100100,
    "icon": "",
    "name": "app版本管理",
    "path": "project/cms/system/app/index",
    "uri": "project/cms/system/app",
    "sort": 0,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100102,
    "parentId": 100100,
    "icon": "",
    "name": "部门管理",
    "path": "project/cms/system/department/index",
    "uri": "project/cms/system/department",
    "sort": 1,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100103,
    "parentId": 100100,
    "icon": "",
    "name": "角色管理",
    "path": "project/cms/system/role/index",
    "uri": "project/cms/system/role",
    "sort": 2,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100104,
    "parentId": 100100,
    "icon": "",
    "name": "菜单管理",
    "path": "project/cms/system/menu/index",
    "uri": "project/cms/system/menu",
    "sort": 3,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100200,
    "parentId": 0,
    "icon": "message",
    "name": "新闻管理",
    "path": "",
    "uri": "",
    "sort": 1,
    "hidden": 1,
    "type": 0,
  },
  {
    "id": 100201,
    "parentId": 100200,
    "icon": "",
    "name": "新闻首页维护",
    "path": "project/cms/news/frontpage/index",
    "uri": "project/cms/news/frontpage",
    "sort": 0,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100202,
    "parentId": 100200,
    "icon": "",
    "name": "置顶新闻",
    "path": "project/cms/news/top/index",
    "uri": "project/cms/news/top",
    "sort": 1,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100203,
    "parentId": 100200,
    "icon": "",
    "name": "专题新闻",
    "path": "project/cms/news/special/index",
    "uri": "project/cms/news/special",
    "sort": 2,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100204,
    "parentId": 100200,
    "icon": "",
    "name": "地方性新闻",
    "path": "project/cms/news/original/index",
    "uri": "project/cms/news/original",
    "sort": 3,
    "hidden": 1,
    "type": 1,
  },
]

处理函数如下:

javascript 复制代码
/**
将数组转为树形
*/
export function ofList(
    data,
    rootIndex,
    itemHandler = item => item,
    current = 'id',
    parentId = 'parentId',
    children = 'children',
  ) {
    const treeData = [];
  
    let index = 0;
  
    while (index < data.length) {
      const item = itemHandler(data[index]);
  
      if (item[parentId] === rootIndex) {
        data.splice(index, 1);
        const childrens = ofList(data, item[current], itemHandler, current, parentId, children);
        item[children] = childrens.length > 0 ? childrens : undefined;
        treeData.push(item);
        index = 0;
      } else {
        index += 1;
      }
    }
  
    return treeData;
}

处理后的数据就是方式一的数据格式。因为最近在写前端,所以就在前端处理一下数据,后期赋上方式一的处理。

相关推荐
放逐者-保持本心,方可放逐9 天前
react 框架应用+总结+参考
前端·前端框架·react
白泽来了9 天前
我开源了一个短视频应用(Go+React)|DouTok2.0 项目介绍
微服务·开源·go·react
星辰大海141210 天前
react 基础学习笔记
前端·javascript·笔记·学习·react·1024程序员节
canonical-entropy11 天前
从React Hooks看React的本质
前端·前端框架·react·hooks·1024程序员节
不知名靓仔11 天前
React常用前端框架合集
前端框架·react
廖秋林12 天前
Vite React 项目绝对路径配置
javascript·typescript·react
长河13 天前
React18-useEffect函数
前端·react·1024程序员节
Casta-mere20 天前
【Next.js 项目实战系列】08-数据处理
typescript·react·next.js·issue tracker
任风雨21 天前
React1-基础概念
前端·react
职教育人22 天前
前端三大框架对比与选择
前端·vue.js·react·angular