To display correctly a Tree view in React, I need to filter a nested tree object.
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"
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
}
]
}
]
}
];