流浪のwolf

卷帝

导航

js递归遍历树形结构数据,获取所有数组id集合

function getAllIds(tree, result) {
  //遍历树  获取id数组
  for (const i in tree) {
    result.push(tree[i].id); // 遍历项目满足条件后的操作
    if (tree[i].children) {
      //存在子节点就递归
      getAllIds(tree[i].children, result);
    }
  }
  return result;
}

 

posted on 2023-07-26 14:23  流浪のwolf  阅读(510)  评论(0编辑  收藏  举报