js将list转化为tree格式的几种写法

boyanx21小时前技术教程1

最近在考虑一个树状结构存储。

最终需要将list转化为tree格式

源数据示例

源数据共401条

[{ "menuId" : "5f50c5fb8f0d74536bbfb7a4", "menuName" : "菜单管理", "parentMenuId" : null },
{ "menuId" : "5f524416ff216c2cbc554907", "menuName" : "频道管理", "parentMenuId" : "5f50c5fb8f0d74536bbfb7a4" },
{ "menuId" : "5f576677d9588f3d78fbdb74", "menuName" : "分类管理", "parentMenuId" : "5f524416ff216c2cbc554907" },
{ "menuId" : "5f588b22499cd2538411b98a", "menuName" : "发布管理", "parentMenuId" : "5f50c5fb8f0d74536bbfb7a4" },
{ "menuId" : "5f588b85499cd2538411b98b", "menuName" : "权限管理", "parentMenuId" : "5f50c5fb8f0d74536bbfb7a4" },
{ "menuId" : "5f588f8358bc0d3e647403a1", "menuName" : "菜单管理", "parentMenuId" : "5f588b85499cd2538411b98b" }
...
]

方法1

递归遍历children

共执行 递归 1612025ms左右时间(win10/i7 8th/16G)

const list = [...]
// 递归 161202 次 5ms左右时间
const list2tree1 = (list, parentMenuId) => {
    return list.filter(item => {
        if (item.parentMenuId === parentMenuId) {
            item.children = list2tree1(list, item.menuId)
            return true
        }
        return false
    })
}
list2tree1(list, null)

方法2

因为方法1是查询的children,所以每次必须全部遍历。

我们换个思路,查询每个节点的parent,查到paret之后,内部循环就可以截止了。(使用find方法)

共执行 689763.6ms左右

const list = [...]
// 68976 次 3.6ms左右
const list2tree2 = (list, parentMenuId) => {
    return list.filter(item => {
        if (item.parentMenuId !== parentMenuId) {
            let parent = list.find(parent => parent.menuId === item.parentMenuId)
            if (!parent.children) parent.children = []
            parent.children.push(item)
            return false
        }
        return true
    })
}
list2tree2(list, null)

方法3

在方法2的基础上,将每次find的parentNode缓存起来,减少相同parent的查询次数

共执行 153371.8ms左右

const list = [...]
// 15337 次 1.8ms左右 cache parent
const list2tree3 = (list, parentMenuId) => {
    let parentObj = {}
    return list.filter(item => {
        if (item.parentMenuId !== parentMenuId) {
            if (!parentObj[item.parentMenuId]) {
                parentObj[item.parentMenuId] = list.find(parent => parent.menuId === item.parentMenuId)
                parentObj[item.parentMenuId].children = []
            }
            parentObj[item.parentMenuId].children.push(item)
            return false
        }
        return true
    })
}
list2tree3(list, null)

方法4

遍历tree之前,先遍历一遍数组,将数据缓存到object中。

二次遍历,直接使用object中的缓存

共执行 8020.2ms左右

const list = [...]
// 802 次 0.2ms左右
const list2tree4 = (list, parentMenuId) => {
    let menuObj = {}
    list.forEach(item => {
        item.children = []
        menuObj[item.menuId] = item
    })
    return list.filter(item => {
        if (item.parentMenuId !== parentMenuId) {
            menuObj[item.parentMenuId].children.push(item)
            return false
        }
        return true
    })
}
标签: tree.js

相关文章

一文带你了解生成树协议三个版本:STP、RSTP 和 MSTP

生成树协议(Spanning Tree Protocol,STP)及其后续改进版,如快速生成树协议(Rapid Spanning Tree Protocol,RSTP)和多生成树协议(Multiple...

D3.js 4.0.0 发布,JavaScript 可视化库

D3 现在是许多库的模块化组件,当然也可以独立使用。D3.js 是基于数据操作文档的 JavaScript库,通过 HTML、SVG 和 CSS 绑定数据。D3 包括可视化组件与数据驱动型的 DOM...

Codeology:那些GitHub项目看上去是什么样子?

支付公司Braintree近日创建超级酷炫的开源代码可视化工具--Codeology,所以用户可以看到GitHub项目在各自产品中如何使用不同的编程语言。该可视化工具已经嵌入至GitHub公共API,...

一次性搞定JS的DOM操作(js中dom)

一.什么是DOM我们知道,无论是现在的开发框架(底层还是操作DOM)还是传统的开发都离不开DOM的操作,所以了解和学习DOM是必须的,也是成为一个全面的前端开发的必备知识和内容,那接下来我们就来看下什...

Vue3远程加载组件(vue引入外部组件)

一、Vite是什么?首先,讲下Vite名字的由来,Vite实际上是法语中快的意思,所以顾名思义,这个工具就是为了给我们带来更快的开发体验,它是一个面向现代浏览器,基于ECMA标准的ES Module实...

初识three.js,搭建three.js+vue.js项目

作者:前端藏经阁转发链接:https://www.yuque.com/xwifrr/uxqg5v/ggxx2bWebGL简介:WebGL(全写Web Graphics Library)是一种3D绘图协...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。