es6 树结构过滤对应id的数据

在 ES6 中,可以使用递归和数组高阶方法来过滤树结构中对应 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) {
  return tree.reduce((acc, node) => {
    if (node.id === id) {
      return [...acc, node];
    } else if (node.children && node.children.length > 0) {
      const filteredChildren = filterTreeById(node.children, id);
      if (filteredChildren.length > 0) {
        return [...acc, { ...node, children: filteredChildren }];
      }
    }
    return acc;
  }, []);
}

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

在这个示例中,我们有一个 tree 数组表示树结构,每个节点包含 id 和 childrenfilterTreeById 函数递归遍历树,并返回一个新的过滤后的树结构,其中只包含指定 ID 的节点及其子节点。以上代码输出的结果会是一个包含符合过滤条件的节点的新树结构。

请注意,这只是一个简单的示例,实际情况中你可能需要根据你的数据结构和需求做一些调整。

在JavaScript中,你可以使用递归来过滤树结构中对应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) {
  return tree.filter(node => {
    if (node.id === id) {
      return true;
    } else if (node.children && node.children.length > 0) {
      node.children = filterTreeById(node.children, id);
      return node.children.length > 0;
    }
    return false;
  });
}

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

在这个示例中,我们有一个tree数组表示树结构,每个节点包含idchildrenfilterTreeById函数递归遍历树,并返回一个新的过滤后的树结构,其中只包含指定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) {
  const filteredData = [];
  
  tree.forEach(node => {
    if (node.id === id) {
      filteredData.push(node);
    } else if (node.children && node.children.length > 0) {
      const filteredChildren = filterTreeById(node.children, id);
      if (filteredChildren.length > 0) {
        filteredData.push({
          ...node,
          children: filteredChildren
        });
      }
    }
  });
  
  return filteredData;
}

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

在这个示例中,我们有一个 tree 数组表示树结构,每个节点都是一个对象,包含 id 和 children 属性。filterTreeById 函数递归遍历树,并返回一个新的过滤后的树结构,其中只包含指定 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(node, id) {
  if (node.id === id) {
    return { ...node };
  } else {
    if (node.children) {
      const filteredChildren = node.children
        .map(child => filterTreeById(child, id))
        .filter(Boolean);
        
      if (filteredChildren.length > 0) {
        return {
          ...node,
          children: filteredChildren
        };
      }
    }
  }
  return null;
}

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

在这个示例中,我们有一个 tree 对象表示树结构,每个节点都是一个包含 id 和 children 属性的对象。filterTreeById 函数递归遍历树,当找到指定 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) {
  return tree.reduce((filteredData, node) => {
    if (node.id === id) {
      filteredData.push(node);
    } else if (node.children && node.children.length > 0) {
      const filteredChildren = filterTreeById(node.children, id);
      if (filteredChildren.length > 0) {
        filteredData.push({
          ...node,
          children: filteredChildren
        });
      }
    }
    return filteredData;
  }, []);
}

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

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

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

请注意,以上代码是基于对象数据的树结构进行过滤。如果你的树结构是由其他方式组织的(例如多叉树),你可能需要根据你的实际数据结构进行相应的调整。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值