js 一维数组转换为树形结构

    // 一维数组处理为树形
    static arrayToTree(arr: any, parentId = null) {
        const tree: any = []
        arr.forEach((node: any) => {
            if (node.parentUIPermissionId === parentId) {
                const children = this.arrayToTree(arr, node.id)
                if (children.length > 0) {
                    node.children = children
                }
                tree.push(node)
            }
        })
        return tree
    }

调用:

this.arrayToTree(res.data)

你可能感兴趣的:(vue.js,javascript,前端)