首页 > 其他分享 >TS:菜单数组转树形,支持多级(递归)

TS:菜单数组转树形,支持多级(递归)

时间:2023-01-24 22:23:00浏览次数:39  
标签:菜单 const tree list TS any item 树形 children

源数组

树形菜单

方法一

import {menus} from './menus.ts'

function listToTree(list: any = [], options = {}, data = null) {
  const { rootWhere, childsWhere, addChilds } = Object.assign(
    { 
      rootWhere: (parent:any,self: any) => {
        return self.parentId === 0
      },
      childsWhere: (parent: any, self: any) => {
        return parent.id === self.parentId
      },
      addChilds: (parent: any, childList: any) => {
        if (childList?.length > 0) {
          parent['children'] = childList
        }
      }
    },
    options || {}
  )
  let tree = [] as any
  // 空列表
  if (!(list?.length > 0)) {
    return tree
  }
  // 顶级
  const rootList = list.filter((item: any) => rootWhere && rootWhere(data, item))
  if (!(rootList?.length > 0)) {
    return tree
  }
  tree = tree.concat(rootList)
  // 子级
  tree.forEach((root: any) => {
    const rootChildList = list.filter((item: any) => childsWhere && childsWhere(root, item))
    if (!(rootChildList?.length > 0)) {
      return
    }
    rootChildList.forEach((item: any) => {
      const childList = listToTree(list, { rootWhere: childsWhere, childsWhere, addChilds }, item)
      addChilds && addChilds(item, childList)
    })
    addChilds && addChilds(root, rootChildList)
  })

  return tree
}

const menuTree = listToTree(menus) // menuTree为最终想要的树形菜单数组

方法二

import {menus} from './menus.ts'

interface menuType {
    id: string | number,
    parentId: string | number,
    children?: menuType[]
    [key: string]: any
}

// datas:后端传过来的菜单数组
// list:当前层级列表
const toTree = (datas: menuType[], list: menuType[]) => {
  list.forEach((item: menuType) => {
    const tmpMenu = { ...item }
    const children = datas.filter((u: menuType) => u.parentId === tmpMenu.id)
    if (children && children.length > 0) {
      item.children = item.children || []
      item.children.push(...children)
      toTree(datas, children)
    }
  })
}
const menuTree = menus.filter(x => x.parentId === 0)
toTree(menus, menuTree) // 调用函数,向menuTree添加children,menuTree为最终想要的树形菜单数组

标签:菜单,const,tree,list,TS,any,item,树形,children
From: https://www.cnblogs.com/zhaoshujie/p/17066449.html

相关文章

  • CInternetSession::OpenURL
    PSSIDNumber:172551ArticleLastModifiedon3/7/2005Theinformationinthisarticleappliesto:TheMicrosoftFoundationClasses(MFC),whenusedwith:Micr......
  • hdu:Simpsons’ Hidden Talents(kmp)
    ProblemDescriptionHomer:Marge,Ijustfiguredoutawaytodiscoversomeofthetalentsweweren’tawarewehad.Marge:Yeah,whatisit?Homer:Takemefor......
  • tss
    代码随想录|Day6-2|LC01两数之和、242.有效的字母异位词1.两数之和解法1,利用HashMap(map.get(Key))实现数的存储和输出classSolution{publicint[]two......
  • ts
    代码随想录|Day6-2|LC01两数之和、242.有效的字母异位词1.两数之和解法1,利用HashMap(map.get(Key))实现数的存储和输出classSolution{publicint[]two......
  • bitset简易使用方法
    何为bitset一个stl,可以大大减少储存布尔数所需的空间,本质上就是个存二进制数的容器具体而言,省的空间是用int存的\(\frac{1}{32}\)示例bitset<N>bi(10111);//括号里的是......
  • [LeetCode] 1828. Queries on Number of Points Inside a Circle
    Youaregivenanarray points where points[i]=[xi,yi] isthecoordinatesofthe ith pointona2Dplane.Multiplepointscanhavethe same coordinat......
  • P4022 [CTSC2012]熟悉的文章 题解
    题目链接简要题意给定\(m\)个模板串和\(n\)个匹配串,如果一个字符串是一个模板串的子串且长度不小于\(L\)则称其为“熟悉的”,对于每个匹配串,求一个最大的\(L\),满足......
  • 基于ACO蚁群算法的tsp优化问题matlab仿真
    1.算法描述“基本原理蚁群算法(AntColonyOptimization,ACO)是一种基于种群寻优的启发式搜索算法,有意大利学者M.Dorigo等人于1991年首先提出。该算法受到自然界真实蚁......
  • 无法加载文件 C:\Users\Administrator\Desktop\spider01\venv\Scripts\activat
    遇到问题原因Restricted(防止运行没有数字签名的脚本),要设置成remotesigned模式解决方案输入get-executionpolicy以管理员的方式打开Powershall运行,并在命令窗......
  • 生成requirements.txt文件
    对于Python项目,生成和使用requirements.txt是十分必要的。通过requirements.txt可以一次性保存和安装项目所需要的所有库。尤其是在不同电脑操作时。requirements.txt的样......