乐趣区

关于javascript:vue中递归出已知节点的所有父级的id

 findAncestry(arr, id) {var temp = []
   var forFn = function (list, id) {for (var i = 0; i < list.length; i++) {var item = list[i]
       if (item.id === id) {temp.unshift(item.id)
         forFn(arr, item.parentId)
         break
       } else {if (item.children) {forFn(item.children, id)
         }
       }
     }
   }
   forFn(arr, id)
   temp.unshift('0')
   return temp
 },

应用

 console.log('aaaaaaaaa',this.findAncestry(this.menuOptions,this.form.pid))
退出移动版