美文网首页
递归重构改造

递归重构改造

作者: 露露璐璐 | 来源:发表于2018-10-19 11:43 被阅读0次

递归重构改造
改造前:

export function refactTreeData (tree, node) {
  if (tree && tree.length > 0) {
    for (const branch of tree) {
      if (branch) {
        for (const key in branch) {
          if (key === node) {
            if (branch[key] && branch[key].length < 1) {
              // console.debug(`refactTreeData find ${key} is null`)
              branch[key] = undefined
            } else {
              refactTreeData(branch[key], key)
            }
            break
          }
        }
      }
    }
  }
  return tree
}

改造后:

export function refactTreeData (tree, node) {
  if (tree && tree.length > 0) {
    for (const branch of tree) {
      if (branch && branch[node] && branch[node].length < 1) {
        branch[node] = undefined
      } else {
        refactTreeData(branch[node], node)
      }
    }
  }
  return tree
}

相关文章

网友评论

      本文标题:递归重构改造

      本文链接:https://www.haomeiwen.com/subject/sgjyzftx.html