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

json:树结构数据

var arrData = [{
"label": "中国",
"City": null,
"value": "",
"children": [{
"label": "河北",
"City": "",
"value": "",
"children": [{
"label": "石家庄",
"City": "",
"value": "1.1",
"children": null
},
{
"label": "保定",
"City": "",
"value": "1.2",
"children": null
},
{
"label": "邯郸",
"City": "",
"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": "",
"value": "",
"children": [{
"label": "济南",
"City": "",
"value": "2.1",
"children": null
}]
},
{
"label": "北京",
"City": "",
"value": "",
"children": null
} ]
}];

递归查找父级数据

   // data:json nodeId:节点,arrRes:[] 返回的数据
function getParentNode(data, nodeId, arrRes) {
if (!data ) {
if (!nodeId) {
arrRes.unshift(nodeId);
}
return arrRes;
}
for (var i = , 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);
}
} }
return arrRes;
};

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

2.再次修改后:

// data:json, nodeId:节点
function getParent(data2, nodeId2) {
var arrRes = [];
if (data2.length == ) {
if (!!nodeId2) {
arrRes.unshift(nodeId2);
}
return arrRes;
}
let rev = (data, nodeId) => {
for (var i = , 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);
}
}
}
return arrRes;
};
arrRes = rev(data2, nodeId2);
return arrRes;
}

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

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

var getValue=(function () {
var arrData = [{
"label": "中国",
"City": null,
"value": "",
"children": [{
"label": "河北",
"City": "",
"value": "hb",
"children": [{
"label": "石家庄",
"City": "",
"value": "1.1",
"children": null
},
{
"label": "保定",
"City": "",
"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": "",
"value": "",
"children": [{
"label": "济南",
"City": "",
"value": "2.1",
"children": null
}]
},
{
"label": "北京",
"City": "",
"value": "",
"children": null
} ]
}];
console.log(JSON.stringify(arrData[]))
return function getValue(values){
var arrs=[];
var values=values;
var c=JSON.stringify(arrData[]);
var keys='';
if(arguments[]&&c.search(values)!=-){
for(var i=;i<;i++){
var newReg=new RegExp('\"City\"\:\"([^\"]+)\"\,\"value\"\:\"'+values);
if(values!=keys){
arrs.unshift(values)
}
if(c.search(newReg)!=-){
keys=c.match(newReg)[].replace(newReg,'$1')
arrs.unshift(keys);
values=keys;
}else{
arrs.unshift(null)
return arrs;
}
}}else{
return values
}
}
})(); console.log(getValue('1.3.1'))

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

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

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

[{
"orgId": ,
"orgName": "校长办公室1",
"parentId": ,
"children": [{
"orgId": ,
"orgName": "校长办公室2",
"parentId": ,
"children": [{
"orgId": ,
"orgName": "校长办公室3",
"parentId": ,
}]
}]
}]
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(); // 0 1 2
findParent(); // 0 1
findParent(); // undefined

5.从上往下找下的

    function getParents(data, id) {
for (var i in data) {
if (data[i].value == id) {
return [];
}
if (data[i].children) {
var ro = getParents(data[i].children, value);
if (ro !== undefined)
return ro.concat(data[i].id);
}
}
console.log(ro);
}

js 递归树结构数据查找父级的更多相关文章

  1. Sqlserver如何递归查询层级数据将父级字段和本级某个字段合并?如何自定义用户函数并调用?

    开门见山,首先说下遇到的问题:前期系统地区字典表中,每个省市县只存了本级名称,没存完整的字段.如:肥西县隶属安徽省合肥市,表中就存了一个肥西县.现有需求需要将完整字段显示,由于系统已在线上运营,无法做 ...

  2. JS - 点击事件排除父级标签

    点击事件排除父级标签,这里使用的是stopPropagation()方法.event.stopPropagation(); 对了,这里还用了解除click事件,unbind. 下面这篇博文,介绍挺全的 ...

  3. JS-对象查找父级

    之前在寻找两个以上的父级,一直傻傻的用parent().parent()... 今天,需要写五个,当然以前也是写过五个的,但是今天总想着换个简单的方式,至少不要.parent().parent().p ...

  4. js动态绑定class(当前父级div下的子元素有没有这个class,有的话移除,没有的话添加)

    <div class="layui-inline" id=‘’   onclick="changeType(id)">                ...

  5. php中递归查找父级名称

    /** * 获取所属公司 * @param array 列表 * @param $id 上级ID * @return array */ private static function get_top_ ...

  6. JS获取节点的兄弟,父级,子级元素的方法(js获取子级获取到换行与空格元素-FF)

    先说一下JS的获取方法,其要比JQUERY的方法麻烦很多,后面以JQUERY的方法作对比. JS的方法会比JQUERY麻烦很多,主要则是因为FF浏览器,FF浏览器会把你的换行也当最DOM元素 < ...

  7. js或jquery如何获取父级、子级、兄弟元素(包括祖级、孙级等)

    原生javascript方法: var a = document.getElementById("dom"); del_space(a); //清理空格 var b = a.chi ...

  8. dom node 查找父级parentNode

    var o = document.querySelectorAll("a[href='baidu.com']"); var p = o[o.length-1];console.lo ...

  9. js根据子目录数目显示父级目录

    需求:<ul>中<li>数量为0,则不显示<ul>以及<b>:<div>中<ul>数量为0,则不显示<div> 1. ...

随机推荐

  1. GRPC .netcore

    GRPC是Google发布的一个开源.高性能.通用RPC(Remote Procedure Call)框架.提供跨语言.跨平台支持.以下以一个.NET Core Console项目演示如何使用GRPC ...

  2. solidity 合约间调用以及参数传递

    在 以太坊中合约间是可以相互调用,并且正常进行参数传递以及返回值处理. contract1.sol pragma solidity ^0.4.0; contract Test1 { uint256 p ...

  3. Ubuntu64 apache2+lvs+Keepalived

    安装 apache2, vim, keepalived 和 ipvsadm 打开 vim /etc/keepalived/keepalived.conf 点击 i, 进入编辑状态, 输入: ! Con ...

  4. [USACO09FEB] 改造路Revamping Trails | [JLOI2011] 飞行路线

    题目链接: 改造路 飞行路线 其实这两道题基本上是一样的,就是分层图的套路题. 为什么是分层图呢?首先,我们的选择次数比较少,可以把这几层的图建出来而不会爆空间.然后因为选择一个边权为0的路线之后我们 ...

  5. React.Component 与 React.PureComponent(React之性能优化)

    前言 先说说 shouldComponentUpdate 提起React.PureComponent,我们还要从一个生命周期函数 shouldComponentUpdate 说起,从函数名字我们就能看 ...

  6. 在CMD中建立一个不能删除的文件

    Windows 下不能够以下面这些字样来命名文件/文件夹,包括:“aux”“com1”“com2”“prn”“con”和“nul”等,因为这些名字都属于设备名称,等价于一个 DOS 设备,如果我们把文 ...

  7. 读取xml文件内容到数据库

    前言 前言不搭后语·················· 内容 听某个大牛说他们的公司常常会涉及到从xml文件中读数据到写入到数据库,序列化的时候会遇到这这个问题,将要持久化的数据到xml文件存储起来, ...

  8. css中设置background属性

    属性解释 background属性是css中应用比较多,且比较重要的一个属性,它是负责给盒子设置背景图片和背景颜色的,background是一个复合属性,它可以分解成如下几个设置项: backgrou ...

  9. 《[MySQL技术内幕:SQL编程》读书笔记

    <[MySQL技术内幕:SQL编程>读书笔记 2019年3月31日23:12:11 严禁转载!!! <MySQL技术内幕:SQL编程>这本书是我比较喜欢的一位国内作者姜承尧, ...

  10. PostMan --API调试工具

    https://blog.csdn.net/fxbin123/article/details/80428216