惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
S
Securelist
MyScale Blog
MyScale Blog
Jina AI
Jina AI
罗磊的独立博客
The Cloudflare Blog
美团技术团队
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
雷峰网
雷峰网
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
博客园 - 聂微东
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
T
Tailwind CSS Blog
Attack and Defense Labs
Attack and Defense Labs
博客园_首页
Latest news
Latest news
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
D
Docker
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
V2EX - 技术
V2EX - 技术
N
News | PayPal Newsroom
Microsoft Security Blog
Microsoft Security Blog

博客园 - 泡面 @ 幸福

nodejs&wsl&vscode&docker开发环境搭建 python中list数组指定类型 windows10安装Trading View出错解决办法 windows or linux 64位安装ta-lib包 在centos7开启SSH服务 在centos7升级nodejs到最新版本 一张自画的RBAC用户、组、角色、权限、指令、资源图 JSON数据格式生成无限级树结构 MYSQL查询上级和下级 码云push时提示 DeployKey does not support push code fatal: Could not read from remote repository. git常用命令 thinkjs框架发布上线PM2管理,静态资源访问配置 登陆服务器提示“You need to run "nvm install N/A" to install it before using it.” CentOS 7.x 用shell增加、删除端口 CentOS 7.X 安全手记 Centos 7.x nginx隐藏版本号 centos7磁盘挂载及取消 CentOS 7.4上网速度慢,修改DNS! Centos7.4 安装Docker
JSON树形格式从子级获取所有父级ID
泡面 @ 幸福 · 2020-02-07 · via 博客园 - 泡面 @ 幸福

elementUI中的Cascader级联选择器组件,在指定选择项时,它需要一个数组值,如:[爷爷, 爸爸, 自己],然后进行v-model绑定这个数组

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>Document</title>
</head>
<body>
<pre>
    // 结构:
// 
//   1 --- 3
//     --- 4 --- 5 --- 6
//                 --- 8
//     --- 7
//     --- 12 --- 13
//   2 --- 9 --- 10
//   11
// 获取节点以及节点的父级关系
// 0 1
// 1 3
// 1 4
// 2 5
// 3 6
// 3 8
// 1 7
// 1 12
// 2 13
// 0 2
// 1 9
// 2 10
// 0 11
</pre>
    <input type="number" id="input">
    <a href="javascript:;" onclick="getArr()">获取</a>
    <div id="result">结果:</div>
<script>
// js 获取树形深度关系数组
// 树形数据如下例中的treeData,
// 希望有如下结果:
// console.log(getTreeDeepArr(1, treeData)); // [1]
// console.log(getTreeDeepArr(3, treeData)); // [1, 3]
// console.log(getTreeDeepArr(5, treeData)); // [1, 4, 5]
var treeData = [{
    id: 1,
    children: [{
        id: 3
    },{
        id: 4,
        children: [{
            id: 5,
            children: [{
                id: 6
            },
            {
                id: 8
            }]
        }]
    },{
        id: 7
    },{
        id: 12,
        children: [{
            id: 13
        }]
    }]
},{
    id: 2,
    children: [{
        id: 9,
        children: [{
            id: 10
        }]
    }]
},{
    id: 11
}];

// 结构:
// 
//   1 --- 3
//     --- 4 --- 5 --- 6
//                 --- 8
//     --- 7
//   2 --- 9 --- 10
//   11
// 获取节点以及节点的父级关系
// 0 1
// 1 3
// 1 4
// 2 5
// 3 6
// 3 8
// 1 7
// 0 2
// 1 9
// 2 10
// 0 11
function getTreeDeepArr(key, treeData) {

    let arr = []; // 在递归时操作的数组
    let returnArr = []; // 存放结果的数组
    let depth = 0; // 定义全局层级
    // 定义递归函数
    function childrenEach(childrenData, depthN) {

        for (var j = 0; j < childrenData.length; j++) {

            depth = depthN; // 将执行的层级赋值 到 全局层级

            arr[depthN] = (childrenData[j].id);
            
            if (childrenData[j].id == key) {

                // returnArr = arr; // 原写法不行, 因 此赋值存在指针关系
                returnArr = arr.slice(0, depthN+1); //将目前匹配的数组,截断并保存到结果数组,
                break

            } else {

                if (childrenData[j].children) {

                    depth ++;
                    childrenEach(childrenData[j].children, depth);

                }
            }

        }
        return returnArr;
    }
    return childrenEach(treeData, depth);
}

function getArr() {
    var _input = document.getElementById('input').value;
    
    console.log(getTreeDeepArr(_input, treeData).join(','))
    document.getElementById('result').innerHTML = '结果:' + getTreeDeepArr(_input, treeData).join(',');
}
console.log(getTreeDeepArr(7, treeData));

</script>
</body>
</html>

核心代码如下

function getTreeDeepArr(key, treeData) {

    let arr = []; // 在递归时操作的数组
    let returnArr = []; // 存放结果的数组
    let depth = 0; // 定义全局层级
    // 定义递归函数
    function childrenEach(childrenData, depthN) {

        for (var j = 0; j < childrenData.length; j++) {

            depth = depthN; // 将执行的层级赋值 到 全局层级

            arr[depthN] = (childrenData[j].id);
            
            if (childrenData[j].id == key) {

                // returnArr = arr; // 原写法不行, 因 此赋值存在指针关系
                returnArr = arr.slice(0, depthN+1); //将目前匹配的数组,截断并保存到结果数组,
                break

            } else {

                if (childrenData[j].children) {

                    depth ++;
                    childrenEach(childrenData[j].children, depth);

                }
            }

        }
        return returnArr;
    }
    return childrenEach(treeData, depth);
}

转自:https://segmentfault.com/a/1190000014827485