js多层嵌套对象数组-根据最里层id找出它所属的每层父级id集合

根据已有子级key,找到它的所有对应的父级们的集合。

  • 比如我们现在有 let keys = [‘group300’,‘group301’,‘group302’],我们要找到它们的父级,然后再找爷级,以此类推。this.parentsKeyList:[]存储它们。

  • 我们先看下初始数据data树的结构:

  const data = {
        "key": "root",
         "type": "root",
         "name": "root",
         "child": [
           {
             "key": "competition01",
             "type": "competition",
             "name": "菜菜大赛",
             "child": [
               {
                 "key": "area100",
                 "type": "area",
                 "name": "华南赛区",
                 "child": [
                   {
                     "key": "contest200",
                     "type": "contest",
                     "name": "挑战赛",
                     "child": [
                       {
                         "key": "group300",
                         "type": "group",
                         "name": "少儿组",
                         "child": []
                       },
                       {
                         "key": "group301",
                         "type": "group",
                         "name": "小学组",
                         "child": []
                       },
                     ]
                   },
                 ]
               },
             ]
           },
           {
             "key": "competition02",
             "type": "competition",
             "name": "花花大赛",
             "child": [
               {
                 "key": "area101",
                 "type": "area",
                 "name": "东北赛区",
                 "child": [
                   {
                     "key": "contest201",
                     "type": "contest",
                     "name": "创意赛",
                     "child": [
                       {
                         "key": "group302",
                         "type": "group",
                         "name": "初中组",
                         "child": []
                       },
                     ]
                   },
                 ]
               },
             ]
           },
           {
             "key": "competition03",
             "type": "competition",
             "name": "坤坤大赛",
             "child": [
               {
                 "key": "area103",
                 "type": "area",
                 "name": "海外赛区",
                 "child": [
                   {
                     "key": "contest202",
                     "type": "contest",
                     "name": "科技赛",
                     "child": [
                       {
                         "key": "group304",
                         "type": "group",
                         "name": "高中组",
                         "child": []
                       },
                     ]
                   },
                 ]
               },
             ]
           },
         ]
       }
//查找父级kye集合
         bindFindKeys(data, val) {
             let [arr,newArr] = [[],[]];
             let list = data.child;
             if (!list || !val) {
               return ''
             }
             let findParent = (item, nodeKey, parentVal) => {
               // console.log(item, nodeKey, parentVal)
               for (let i = 0; i < item.length; i++) {
                 let node = item[i];
                 if (node.key === nodeKey) {
                   arr.unshift(nodeKey);
                   if (nodeKey === list[0].key) {
                     break
                   }
                   findParent(list, parentVal);
                   break
                 } else {
                   if (node.child) {
                     findParent(node.child, nodeKey, node.key);
                   }
                   continue
                 }
               }
               return arr;
             }
             newArr = findParent(list, val);
 			this.parentsKeyList = Array.from(new Set([...this.parentsKeyList,...newArr]));
             return this.parentsKeyList;  //存储去重一下
          },

使用:

//假如我们要找keys里面的父级key们
 let keys = ['group300','group301','group302'];
  for(let v of keys){
    this.bindFindKeys(data,v);
  }

打印一下:
在这里插入图片描述

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值