关于javascript:js-递归-查找一个值-并返回-所有父级

//utils.js
export const checkList = (list, code, path = []) => {
  if (!list) return []
  for (const data of list) {
    path.push(data.value)
    if (data.value == code) return path
    if (data.children && data.children.length) {
      const findchildren = checkList(data.children, code, path)
      if (findchildren.length) return findchildren
    }
    path.pop()
  }
  return []
}

引入应用

import { checkList } from '../utils/utils.js'
let arr = checkList( list , code ).filter(Boolean)

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理