1.请封装一个js方法,遍历树结构,当节点的children为空时,删除children


var tree = [
{
a:1,
b:2,
children:[
{
a-1:111,
children:[]
}
]
},
{
a:2,
b:2,
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
    }

function transform(arr) {
arr = arr.map(item => {
const { children } = item
if (children && children.length == 0) delete item.children
else if(children)item.children = transform(children)
return item
});
return arr;
};


// 递归删除空数组
export function deleteEmptyArr(arr, arrKey) {
  if (arr && arr.length) {
    arr.forEach((item) => {
      if (item[arrKey] && !item[arrKey].length) {
        delete item[arrKey]
      } else if (item[arrKey] && item[arrKey].length) {
        handleArr(item[arrKey], arrKey)
      }
    })
  }
  return arr
}
//--------------------------------
  var tree = [
    {
      a: 1,
      b: 2,
      children: [
        {
          'a-1': 111,
          children: [],
        },
      ],
    },
    {
      a: 2,
      b: 2,
      children: [],
    },
  ]
//调用
const resultArr = deleteEmptyArr(tree,'children');
console.log(resultArr ); //最终结果

可否解决这个问题?