简体   繁体   English

如何在嵌套树对象中进行过滤而不丢失javascript中的结构?

[英]How filter in a nested tree object without losing structure in javascript?

To display correctly a Tree view in React, I need to filter a nested tree object. 为了在React中正确显示树视图,我需要过滤一个嵌套的树对象。

I find this post : filter nested tree object without losing structure 我发现这篇文章: 过滤嵌套的树对象而不丢失结构

But actually I would like to do the exactly opposite. 但是实际上我想做相反的事情。 For instance, if in my filterData function name === "a3 " I would like to keep object with name === "a3" 例如,如果在我的filterData函数name === "a3 ”中,我想保留name === "a3"对象

const result = filterData(items, "a3")
const items = [
  {
    name: "a1",
    id: 1,
    children: [
      {
        name: "a2",
        id: 2,
        children: [
          {
            name: "a3",
            id: 3
          },
          {
            name: "a5",
            id: 4
          }
        ]
      }
    ]
  },
  {
    name: "b2",
    id: 2,
    children: [
      {
        name: "a2",
        id: 2,
        children: [
          {
            name: "a3",
            id: 3
          }
        ]
      },
      {
        name: "a4",
        id: 8
      }
    ]
  }
];
const result = [
  {
    name: "a1",
    id: 1,
    children: [
      {
        name: "a2",
        id: 2,
        children: [
          {
            name: "a3",
            id: 3
          }
        ]
      }
    ]
  },
  {
    name: "b2",
    id: 2,
    children: [
      {
        name: "a2",
        id: 2,
        children: [
          {
            name: "a3",
            id: 3
          }
        ]
      }
    ]
  }
];

You could create new objects without mutating the given data and reduce the arrays. 您可以创建新对象而无需更改给定数据并减少数组。

 function filter(array, name) { return array.reduce((r, { children = [], ...o }) => { if (o.name === name) { r.push(o); return r; } children = filter(children, name); if (children.length) { r.push(Object.assign(o, { children })); } return r; }, []); } var items = [{ name: "a1", id: 1, children: [{ name: "a2", id: 2, children: [{ name: "a3", id: 3 }, { name: "a5", id: 4 }] }] }, { name: "b2", id: 2, children: [{ name: "a2", id: 2, children: [{ name: "a3", id: 3 }] }, { name: "a4", id: 8 }] }]; console.log(filter(items, "a2")); console.log(filter(items, "a3")); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

I've just improved the Nina's proposal in order to remove the under levels if "name" isn't include. 我刚刚改进了Nina的建议,以便在不包括“名称”的情况下删除不足的级别。

 function filter(array, name) { return array.reduce((r, o) => { var children; children = filter(o.children || [], name); if (children.length) { r.push(Object.assign({}, o, { children })); } if (o.name === name) { r.push(o); if (o.children && o.children.name !== name) { r[0].children = []; } } return r; }, []); } var items = [{ name: "a1", id: 1, children: [{ name: "a2", id: 2, children: [{ name: "a3", id: 3 }, { name: "a5", id: 4 }] }] }, { name: "b2", id: 2, children: [{ name: "a2", id: 2, children: [{ name: "a3", id: 3 }] }, { name: "a4", id: 8 }] }], result = filter(items, "a2"); console.log(result); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM