相关文章推荐
憨厚的小马驹  ·  java ...·  4 天前    · 
奋斗的豆腐  ·  sys.all_objects ...·  11 月前    · 
   // data:json nodeId:节点,arrRes:[] 返回的数据
    function getParentNode(data, nodeId, arrRes) {
        if (!data ) {
            if (!nodeId) {
                arrRes.unshift(nodeId);
            return arrRes;
        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);
        return arrRes;
View Code

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

2.再次修改后:

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

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

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

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]))
    return function 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)
                    return arrs;
        }}else{
                return values
})();
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 

"orgId": 1, "orgName": "校长办公室1", "parentId": 0, "children": [{ "orgId": 2, "orgName": "校长办公室2", "parentId": 1, "children": [{ "orgId": 3, "orgName": "校长办公室3", "parentId": 2, View Code
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.从上往下找下的

    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);
View Code