J'ai un JSON structuré en arbre qui devrait être filtré et le résultat devrait conserver la structure arborescente.Comment filtrer efficacement l'arborescence en conservant sa structure existante?
var tree = [
{
text: "Parent 1",
nodes: [
{
text: "Child 1",
type: "Child",
nodes: [
{
text: "Grandchild 1"
type: "Grandchild"
},
{
text: "Grandchild 2"
type: "Grandchild"
}
]
},
{
text: "Child 2",
type: "Child"
}
]
},
{
text: "Parent 2",
type: "Parent"
},
{
text: "Parent 3",
type: "Parent"
}
];
Exemple:
1) Si la requête de recherche est parent 1
Résultat attendu:
[
{
text: "Parent 1",
nodes: [
{
text: "Child 1",
type: "Child",
nodes: [
{
text: "Grandchild 1"
type: "Grandchild"
},
{
text: "Grandchild 2"
type: "Grandchild"
}
]
},
{
text: "Child 2",
type: "Child"
}
]
}
]
2) Si la requête de recherche est l'enfant 1
Résultat attendu:
[
{
text: "Parent 1",
nodes: [
{
text: "Child 1",
type: "Child",
nodes: [
{
text: "Grandchild 1"
type: "Grandchild"
},
{
text: "Grandchild 2"
type: "Grandchild"
}
]
}
]
}
]
3) Si la requête de recherche est Grandchild 2
Résultat attendu:
[
{
text: "Parent 1",
nodes: [
{
text: "Child 1",
type: "Child",
nodes: [
{
text: "Grandchild 2"
type: "Grandchild"
}
]
}
]
}
]
Je dois conserver la structure de l'arbre en fonction du niveau de noeud (type ici). Jusqu'à présent, j'ai essayé de filtrer de manière récursive mais je n'ai pas pu recadrer les résultats. Mon JSON est assez grand et donc le remappage basé sur les types devrait se faire dans le filtre lui-même. Veuillez nous contacter.