当前位置: 首页 > news >正文

vue javascript tree 层级数据处理

层级数据是有父子关系的数组,示例:

const treeData = [{id: '1b7e8e98cb1d4a1f81e4fe2dfd9a8458',name: '层级1',parentId: null,children: [{id: '0d45dd5bb4c14d64a3ab0b738add4b24',name: '层级1-1',parentId: '1b7e8e98cb1d4a1f81e4fe2dfd9a8458',children: [{id: 'd559c08b408b46e08fc66ad6e653425d',name: '层级1-1-1',parentId: '1b7e8e98cb1d4a1f81e4fe2dfd9a8458',},{id: '83bdbc6a873842d69b849532c68aa1d2',name: '层级1-1-2',parentId: '1b7e8e98cb1d4a1f81e4fe2dfd9a8458',},],},{id: 'edbaec28dde842a781cdfd9c3df1d6a0',name: '层级1-2',parentId: '1b7e8e98cb1d4a1f81e4fe2dfd9a8458',},],},{id: 'eb6005ef3c634921b20d4dd368934da3',name: '层级2',parentId: null,children: [{id: 'e8ce379630824bf39e6b16c6c3b103d8',name: '层级2-1',parentId: 'eb6005ef3c634921b20d4dd368934da3',},],},{id: '13052d2aaace4be6928e207199453dfc',name: '层级3',parentId: null,},
];

扁平化数据是一个一维数组,示例:

const array = [{"id": "1b7e8e98cb1d4a1f81e4fe2dfd9a8458","name": "层级1","parentId": null},{"id": "0d45dd5bb4c14d64a3ab0b738add4b24","name": "层级1-1","parentId": "1b7e8e98cb1d4a1f81e4fe2dfd9a8458"},{"id": "d559c08b408b46e08fc66ad6e653425d","name": "层级1-1-1","parentId": "0d45dd5bb4c14d64a3ab0b738add4b24"},{"id": "83bdbc6a873842d69b849532c68aa1d2","name": "层级1-1-2","parentId": "0d45dd5bb4c14d64a3ab0b738add4b24"},{"id": "edbaec28dde842a781cdfd9c3df1d6a0","name": "层级1-2","parentId": "1b7e8e98cb1d4a1f81e4fe2dfd9a8458"},{"id": "eb6005ef3c634921b20d4dd368934da3","name": "层级2","parentId": null},{"id": "e8ce379630824bf39e6b16c6c3b103d8","name": "层级2-1","parentId": "eb6005ef3c634921b20d4dd368934da3"},{"id": "13052d2aaace4be6928e207199453dfc","name": "层级3","parentId": null}
];

层级数据转换成扁平化数据

const generateList = (tree) => {let dataList = [];const getList = (data, parentId) => {for (const item of data) {dataList.push({id: item.id,name: item.name,parentId,});if (item.children) {getList(item.children, item.id);}}}getList(tree, null);return dataList;
};const array = generateList(treeData);
console.log(array);

在这里插入图片描述

找到某一个子元素的所有祖先元素

const getAncestor= (tree, id) => {const ancestor = [];const getParent = (tree, id) => {for (const item of tree) {if (item.id === id) {ancestor.unshift({ id: item.id, name: item.name });return true;}if (item.children && item.children.length > 0 && getParent(item.children, id)) {ancestor.unshift({ id: item.id, name: item.name });return true;}}return false;};getParent(tree, id);return ancestor;
};const child = {id: '83bdbc6a873842d69b849532c68aa1d2',name: '层级1-1-2',
};
const ancestors = getAncestor(treeData, child.id);
console.log(ancestors);

在这里插入图片描述

找到某一个父元素的所有子元素们

const getDeepChildren = (tree) => {const array = [];const getChildren = (tree) => {tree.forEach(({ id, name, children }) => {array.push({ id, name });if (children) {getDeepChildren(tree);}});}getChildren(tree);return array;
};
const getChildren = (tree, childId) => {const array = [];const getChildren = (tree, childId) => {tree.forEach(({ id, name, parentId, children }) => {if (parentId === childId) {array.push({ id, name });if (children) {const allChildren = getDeepChildren(children);array.push(...allChildren);}} else if (children) {getChildren(children, childId);}});};getChildren(tree, childId);return array;
};const parent = {id: '1b7e8e98cb1d4a1f81e4fe2dfd9a8458',name: '层级1',
};
const children = getChildren(treeData, parent.id);
console.log(children);

在这里插入图片描述

http://www.lryc.cn/news/260867.html

相关文章:

  • WPF仿网易云搭建笔记(4):信息流控制之消息订阅
  • 持续集成交付CICD:GitLabCI操作Harbor仓库
  • [C++]——学习模板
  • 大数据技术14:FlinkCDC数据变更捕获
  • SpringDataRedis 基本使用
  • 蓝牙物联网智慧工厂解决方案
  • html的学习笔记
  • 每日一道算法题 8(2023-12-16)
  • Unity项目优化案例二
  • 如何发布自定义 npm 组件包
  • iOS_给View的部分区域截图 snapshot for view
  • 计算机网络——数据链路层-可靠传输的实现机制:回退N帧协议GBN(无差错情况、累积确认、有差错情况、发送窗口尺寸)
  • IDEA debug窗口左边工具栏隐藏与显示
  • WPF 基于TableControl的页面切换
  • Lua 元表,元方法
  • C# WPF上位机开发(利用tcp/ip网络访问plc)
  • Knife4j 接口文档如何设置 Authorization 鉴权参数?
  • CentOS 防火墙管理及使用的redis基本常用命令
  • 路由器原理
  • 采埃孚4D成像雷达拆解
  • 若依框架springboot——修改前端图片上传样式
  • mysql 数据库 关于库的基本操作
  • 【通用】Linux,VSCode,IDEA,Eclipse等资源相对位置
  • 音视频技术开发周刊 | 323
  • STM32在CTF中的应用和快速解题
  • SaaS 电商设计 (五) 私有化部署-实现 binlog 中间件适配
  • Android APP 常见概念与 adb 命令
  • 菜鸟学习日记(python)——函数
  • 垃圾回收 (GC) 在 .NET Core 中是如何工作的?
  • Appium 图像识别技术 OpenCV