js 递归树结构数据查找父级

摘要:
node.children){rev;}}}returnarrRes;};arrRes=rev;returnarrRes;}ViewCode调用:varres=getParent;3.正则表达式查找的,递归20级深度的;vargetValue=(function(){vararrData=[{"label":"中国","City":null,"value":"0","children":[{"label":"河北","City":"0","value":"hb","children":[{"label":"石家庄","City":"1","value":"1.1","children":null},{"label":"保定","City":"1","value":"1.2","children":null},{"label":"邯郸","City":"n","value":"bk","children":[{"label":"邯山区","City":"bk","value":"1.3.1","children":[{"label":"丛西街道","City":"1.3.1","value":"1.3.1.1","children":null}]},{"label":"涉县","City":"1.3","value":"1.3.2","children":null},{"label":"丛台区","City":"1.3","value":"1.3.3","children":null}]}]},{"label":"山东","City":"0","value":"2","children":[{"label":"济南","City":"2","value":"2.1","children":null}]},{"label":"北京","City":"0","value":"3","children":null}]}];console.log(JSON.stringify(arrData[0]))returnfunctiongetValue(values){vararrs=[];varvalues=values;varc=JSON.stringify(arrData[0]);varkeys='';if(arguments[0]&&c.search(values)!=-1){keys=c.match[0].replacearrs.unshift;values=keys;}else{arrs.unshiftreturnarrs;}}}else{returnvalues}}})();console.logViewCode4.Cascader级联选择组件v-model的数据是一个数组类型,工作中如果需要回显的话,就需要传递所有父级元素的ID所组成的数组,但是后台只存放了目标元素的ID,所以只能自己去遍历数据获取了用递归查找到ID的所属元素

1.json树数据查找所有父级--完成

json:树结构数据

js 递归树结构数据查找父级第1张js 递归树结构数据查找父级第2张
var arrData =[{
    "label": "中国",
    "City": null,
    "value": "0",
    "children": [{
            "label": "河北",
            "City": "0",
            "value": "1",
            "children": [{
                    "label": "石家庄",
                    "City": "1",
                    "value": "1.1",
                    "children": null
                },
                {
                    "label": "保定",
                    "City": "1",
                    "value": "1.2",
                    "children": null
                },
                {
                    "label": "邯郸",
                    "City": "1",
                    "value": "1.3",
                    "children": [{
                            "label": "邯山区",
                            "City": "1.3",
                            "value": "1.3.1",
                            "children": [{
                                "label": "丛西街道",
                                "City": "1.3.1",
                                "value": "1.3.1.1",
                                "children": null
                            }]
                        },
                        {
                            "label": "涉县",
                            "City": "1.3",
                            "value": "1.3.2",
                            "children": null
                        },
                        {
                            "label": "丛台区",
                            "City": "1.3",
                            "value": "1.3.3",
                            "children": null
                        }
                    ]
                }
            ]
        },
        {
            "label": "山东",
            "City": "0",
            "value": "2",
            "children": [{
                "label": "济南",
                "City": "2",
                "value": "2.1",
                "children": null
            }]
        },
        {
            "label": "北京",
            "City": "0",
            "value": "3",
            "children": null
        }
    ]
}];
View Code

递归查找父级数据

js 递归树结构数据查找父级第1张js 递归树结构数据查找父级第4张
   //data:json nodeId:节点,arrRes:[] 返回的数据
function getParentNode(data, nodeId, arrRes) {
        if (!data ) {
            if (!nodeId) {
                arrRes.unshift(nodeId);
            }
            returnarrRes;
        }
        for (var i = 0, length = data.length; i < length; i++) {
            let node =data[i];
            if (node.value ==nodeId) {
                arrRes.unshift(nodeId);
                console.log(arrData);
                getParentNode(arrData, node.City, arrRes);
                break;
            }
            else{
                if (!!node.children) {
                    getParentNode(node.children, nodeId, arrRes);
                }
            }
        }
        returnarrRes;
    };
View Code

调用: var res = getParentNode(arrData, '1.3.1', []);

2.再次修改后:

js 递归树结构数据查找父级第1张js 递归树结构数据查找父级第6张
//data:json, nodeId:节点
function getParent(data2, nodeId2) {
        var arrRes =[];
        if (data2.length == 0) {
            if (!!nodeId2) {
                arrRes.unshift(nodeId2);
            }
            returnarrRes;
        }
        let rev = (data, nodeId) =>{
            for (var i = 0, length = data.length; i < length; i++) {
                let node =data[i];
                if (node.value ==nodeId) {
                    arrRes.unshift(nodeId);
                    rev(data2, node.City);
                    break;
                }
                else{
                    if (!!node.children) {
                        rev(node.children, nodeId);
                    }
                }
            }
            returnarrRes;
        };
        arrRes =rev(data2, nodeId2);
        returnarrRes;
    }
View Code

调用:var res =getParent(arrData, '1.3.1');

3. 正则表达式 查找的,递归20级深度的;

js 递归树结构数据查找父级第1张js 递归树结构数据查找父级第8张
var getValue=(function () {
    var arrData =[{
    "label": "中国",
    "City": null,
    "value": "0",
    "children": [{
            "label": "河北",
            "City": "0",
            "value": "hb",
            "children": [{
                    "label": "石家庄",
                    "City": "1",
                    "value": "1.1",
                    "children": null
                },
                {
                    "label": "保定",
                    "City": "1",
                    "value": "1.2",
                    "children": null
                },
                {
                    "label": "邯郸",
                    "City": "n",
                    "value": "bk",
                    "children": [{
                            "label": "邯山区",
                            "City": "bk",
                            "value": "1.3.1",
                            "children": [{
                                "label": "丛西街道",
                                "City": "1.3.1",
                                "value": "1.3.1.1",
                                "children": null
                            }]
                        },
                        {
                            "label": "涉县",
                            "City": "1.3",
                            "value": "1.3.2",
                            "children": null
                        },
                        {
                            "label": "丛台区",
                            "City": "1.3",
                            "value": "1.3.3",
                            "children": null
                        }
                    ]
                }
            ]
        },
        {
            "label": "山东",
            "City": "0",
            "value": "2",
            "children": [{
                "label": "济南",
                "City": "2",
                "value": "2.1",
                "children": null
            }]
        },
        {
            "label": "北京",
            "City": "0",
            "value": "3",
            "children": null
        }
    ]
}];
console.log(JSON.stringify(arrData[0]))
    returnfunction getValue(values){
        var arrs=[];
        var values=values;
        var c=JSON.stringify(arrData[0]);
        var keys='';
        if(arguments[0]&&c.search(values)!=-1){
        for(var i=0;i<20;i++){
            var newReg=new RegExp('"City":"([^"]+)"\,"value":"'+values);
                if(values!=keys){
                    arrs.unshift(values)
                }
                if(c.search(newReg)!=-1){
                    keys=c.match(newReg)[0].replace(newReg,'$1')
                    arrs.unshift(keys);
                    values=keys;
                }else{
                    arrs.unshift(null)
                    returnarrs;
                }
        }}else{
                returnvalues
        }
    }
})();
console.log(getValue('1.3.1'))
View Code

4.Cascader 级联选择 组件 v-model的数据是一个数组类型,工作中如果需要回显的话,就需要传递所有父级元素的ID所组成的数组,但是后台只存放了目标元素的ID,所以只能自己去遍历数据获取了

用递归查找到ID的所属元素,然后把每一级的parentId一起返回。

转:https://blog.csdn.net/weixin_42869548/article/details/82012316

js 递归树结构数据查找父级第1张js 递归树结构数据查找父级第10张
[{
    "orgId": 1,
    "orgName": "校长办公室1",
    "parentId": 0,
    "children": [{
        "orgId": 2,
        "orgName": "校长办公室2",
        "parentId": 1,
        "children": [{
            "orgId": 3,
            "orgName": "校长办公室3",
            "parentId": 2,
        }]
    }]
}]
View Code
js 递归树结构数据查找父级第1张js 递归树结构数据查找父级第12张
function buildParentList(arr){
    arr.forEach(g =>
     {
        if(g.parentId !=undefined) {
            let pid = g['parentId']
               let oid = g['orgId']
            parentList[oid] =pid    
        }
        if (g.children !=undefined)
            buildParentList(g['children'])
    })
}
function findParent(idx){
    if (parentList[idx] !=undefined){
        let pid =parentList[idx]
        console.log(pid)
        findParent(pid)
    }
}
buildParentList(list)
findParent(3); //0 1 2
findParent(2); //0 1
findParent(4); //undefined
View Code

5.从上往下找下的

js 递归树结构数据查找父级第1张js 递归树结构数据查找父级第14张
function getParents(data, id) {
        for (var i indata) {
            if (data[i].value ==id) {
                return[];
            }
            if(data[i].children) {
                var ro =getParents(data[i].children, value);
                if (ro !==undefined)
                    returnro.concat(data[i].id);
            }
        }
        console.log(ro);
    }
View Code

免责声明:文章转载自《js 递归树结构数据查找父级》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C# 删除文件delphi 怎么将一个文件流转换成字符串(String到流,String到文件,相互转化)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JS 将UTC时间转为本地时间

在企业实际开发中,有时数据库会存UTC时间,要求不同地区的人访问可以智能识别国家,显示当地时间,下面用的是moment.js转化时间。 下载地址:https://momentjs.com/ js 转化: //初始化日期(utc->local) function dateFormatter(value) { var date = moment.pars...

HBase实践案例:车联网监控系统

项目背景 本项目为车联网监控系统,系统由车载硬件设备、云服务端构成。车载硬件设备会定时采集车辆的各种状态信息,并通过移动网络上传到服务器端。服务器端接收到硬件设备发送的数据首先需要将数据进行解析,校验,随后会将该消息转发到国家汽车监测平台和地方汽车监测平台,最后将解析后的明文数据和原始报文数据存储到系统中。车辆的数据和其他数据需要通过web页面或rest...

GIS中的数据库.gdb与.mdb的区别

gdb是文件地理数据库,mdb是个人地理数据库,都是数据库文件类型。 个人地理数据库,是以access数据库为基础的个人将数据库格式mdb,可以存储不超过2G的文件,只适合Windows系统下; 文件数据库是在文件系统文件夹中保存的各种类型的GIS数据集的集合。 参考文章 GIS中的数据库.gdb与.mdb的区别在哪,m892832piczpec5。...

如何判断数据库中存储的是不是乱码

开发人员说从数据库中读取的是??? 数据库表字符集都是utf8,也set names utf8了,为什么读取到的还是??? 可以判断数据库中存储的是???了,如何验证呢? 1.暂时打开general_log,看看开发人员插入的到底是什么语句 2.抓包分析 字符集测试情况,操作系统字符集为utf8 表的字符集 set names 存中文 读取中文 l...

jquery----语法扩展(导入js文件)

简单使用 第一步,新建js文件 第二步,在js文件中添加 $.extend({ "GDP": function () { console.log("哈哈哈哈"); } }); 第三步,在html中使用   $.GDP()  即可 复杂(1,希望一些函数不可以被外部引用,不可以被修改$) (functi...

BUI 框架使用指南

  指南说明:只适用于对框架的剥离 如果不需要剥离则原来的东西直接粘贴就行  在主界面中使用时需要加入一下引用bui.js jquery.js config.js 末尾的文件 BUI.use(位置1, function ()  其中位置一需要填写你的main-min.js 文件所在的路径以及main的文件名 例如我的文件在Resouce/JS/main-...