封装一个用来获取多层数组对象的最后一层对象集合

发布时间 2023-09-15 11:20:56作者: 流浪のwolf
// 获取多层数组对象的最后一层的对象
function getAllIds(tree:any, result:any) {
  //遍历树  获取id数组
  for (const i in tree) {
    if(tree[i].id) result.push(tree[i]); // 遍历项目满足条件后的操作
    if (tree[i].children) {
      //存在子节点就递归
      getAllIds(tree[i].children, result);
    }
  }
  return result;
}