JS删除树形结构中childKey数组为空的数据

// 删除树形结构中childKey数组为空的数据 tree是目标树形数据,childKey是字段名
export function removeEmptyChildKey(tree, childKey) {
  tree.forEach(item => {
    if (item[childKey] && item[childKey].length === 0) {
      delete item[childKey];
    } else if (item[childKey] && item[childKey].length > 0) {
      item[childKey] = removeEmptyChildKey(item[childKey]);
    }
  })
  return tree;
}