js 树结构数组过滤对应id的不包含父节点的节点对象

如果你想过滤树结构数组中对应ID的节点对象,并且不包含其父节点,可以使用递归和数组高阶方法来实现。下面是一个示例代码:

const tree = [
  {
    id: 1,
    name: 'Root',
    children: [
      {
        id: 2,
        name: 'Child 1',
        children: []
      },
      {
        id: 3,
        name: 'Child 2',
        children: [
          {
            id: 4,
            name: 'Grandchild 1',
            children: []
          }
        ]
      }
    ]
  }
];

function filterTreeById(tree, id, parentId = null) {
  return tree.reduce((filteredData, node) => {
    if (node.id === id && node.parentId === parentId) {
      filteredData.push(node);
    } else if (node.children && node.children.length > 0) {
      const filteredChildren = filterTreeById(node.children, id, node.id);
      if (filteredChildren.length > 0) {
        filteredData.push(...filteredChildren);
      }
    }
    return filteredData;
  }, []);
}

const filteredTree = filterTreeById(tree, 4);
console.log(filteredTree);

在这个示例中,我们有一个 tree 数组表示树结构,每个节点都是一个对象,包含 idparentId 和 children 属性。filterTreeById 函数递归遍历树,并返回一个新的过滤后的树结构数组,其中只包含指定 ID 的节点对象,并且不包含其父节点。

函数 filterTreeById 使用 reduce 方法遍历原始树结构数组,并返回一个新的数组 filteredData。在每一次迭代中,它检查当前节点是否与指定的 ID 匹配,并且其 parentId 与指定的父节点ID匹配。如果匹配,则将该节点添加到 filteredData 中。如果当前节点有子节点,则递归调用 filterTreeById 进一步过滤子节点,并将过滤后的节点数组展开(使用展开运算符 ...)并添加到 filteredData 中。最终返回的结果就是新的过滤后的树结构数组。

请注意,以上代码假设每个节点对象都有一个 parentId 属性,用于指示其父节点的ID。如果你的树结构数组中的节点对象没有这个属性,你可能需要根据你的实际数据结构进行相应的调整。

如果你想基于节点ID过滤树结构数组,返回不包含父节点的节点对象,可以使用递归和深度优先搜索来实现。下面是一个示例代码:

const tree = [
  {
    id: 1,
    name: 'Root',
    children: [
      {
        id: 2,
        name: 'Child 1',
        children: []
      },
      {
        id: 3,
        name: 'Child 2',
        children: [
          {
            id: 4,
            name: 'Grandchild 1',
            children: []
          }
        ]
      }
    ]
  }
];

function filterTreeByNodeId(tree, nodeId, parentFound = false) {
  let filteredData = [];

  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];

    if (node.id === nodeId && !parentFound) {
      filteredData.push(node);
    }

    if (node.children && node.children.length > 0) {
      const filteredChildren = filterTreeByNodeId(node.children, nodeId, parentFound || node.id === nodeId);
      if (filteredChildren.length > 0) {
        filteredData = filteredData.concat(filteredChildren);
      }
    }
  }

  return filteredData;
}

const filteredTree = filterTreeByNodeId(tree, 4);
console.log(filteredTree);

在这个示例中,我们有一个 tree 数组表示树结构,每个节点都是一个对象,包含 id 和 children 属性。filterTreeByNodeId 函数递归遍历树,并返回一个新的过滤后的树结构数组,其中只包含指定节点ID的节点对象,并且不包含其父节点。

函数 filterTreeByNodeId 使用深度优先搜索来遍历树结构数组。它通过一个布尔标志 parentFound 来跟踪是否已经找到了包含指定节点的父节点。如果当前节点的 id 与指定的节点ID匹配,并且尚未找到包含它的父节点,则将该节点添加到 filteredData 数组中。然后,它会递归调用 filterTreeByNodeId 函数来继续搜索当前节点的子节点,将其中符合条件的节点对象连接到 filteredData 数组中。最终返回的结果就是新的过滤后的树结构数组。

请注意,以上代码假设节点对象中的 id 属性是唯一的,而且每个节点对象都没有父节点的引用。如果你的数据结构与此不同,你可能需要相应地自定义代码。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值