sailei1 / blog

1 stars 0 forks source link

array 深度查找 #107

Closed sailei1 closed 3 years ago

sailei1 commented 3 years ago
findItemById(id,array,key,childKey){
              let rs=[];
             function find(id,arr,key,childKey) {
                if(rs.length>0){return}
                if(!Array.isArray(arr)){return}
                let  obj= arr.find((item) => {
                  return item[key]==id;
                })
                if(!obj){
                  return arr.forEach((item)=>{
                    find(id,item[childKey],key,childKey,obj)
                  })
                }else{
                  rs.push(obj);
                }

              }
              find(id,array,key,childKey);
             return rs[0];
           },
let obj = this.findItemById(permissionId, target, 'id', 'children')