zoukankan      html  css  js  c++  java
  • 遍历树结构,当节点的children为空时,删除children

    需求

    如果children为空数组,则删除children

    数据结构

    let arr2=[{
              label: '一级 1',
              children: [{
                label: '二级 1-1',
                children: []
              }]
            }, {
              label: '一级 2',
              children: [{
                label: '二级 2-1',
                children: [{
                  label: '三级 2-1-1'
                }]
              }, {
                label: '二级 2-2',
                children: [{
                  label: '三级 2-2-1'
                }]
              }]
            }, {
              label: '一级 3',
              children: []
            }]

    函数

     // children为[],则删除children键
      function deleteChildren(arr) {
          let childs = arr
          for (let i = childs.length; i--; i > 0) {
            if (childs[i].children) {
              if (childs[i].children.length) {
                this.deleteChildren(childs[i].children)
              } else {
                delete childs[i].children
              }
            }
          }
          return arr
        }

    调用

    let arrNew = deleteChildren(arr2)
    console.log(arrNew)



    转载自::https://www.jianshu.com/p/555a176bd8ee

  • 相关阅读:
    Go 指针
    Go 字符串
    Go Maps
    Go 可变参数函数
    Go 数组和切片
    pyqt5实现窗口跳转并关闭上一个窗口
    spy++查找窗口句柄
    Python中Tk模块简单窗口设计
    pyqt5无边框拖动
    pyqt5 GUI教程
  • 原文地址:https://www.cnblogs.com/linhongwenBlog/p/13847734.html
Copyright © 2011-2022 走看看