当前位置: 首页 > Web前端 > vue.js

js递归找一个值返回所有parents

时间:2023-04-01 01:30:52 vue.js

//utils.jsexportconstcheckList=(list,code,path=[])=>{if(!list)return[]for(constdataoflist){path.push(data.value)if(data.value==code)返回路径if(data.children&&data.children.length){constfindchildren=checkList(data.children,code,path)if(findchildren.length)returnfindchildren}path.pop()}return[]}importuseimport{checkList}from'../utils/utils.js'让arr=checkList(list,code).filter(Boolean)