I want to build a search function which can result in returning the search response in deeply nested hierarchy.
Type of data I am having::
[
{
label: "Navigation",
accordion: true,
key: "navigation",
actions: [
{
action: "navigateTo",
label: "Navigate To"
},
{
action: "Back",
label: "Back"
},
{
action: "closeDialog",
label: "Close Dialog"
},
{
action: "delay",
label: "Delay"
}
]
}]
and I am searching "Delay" it will returns the following result.
[
{
label: "Navigation",
accordion: true,
key: "navigation",
actions: [
{
action: "delay",
label: "Delay"
}
]
}]
If I am searching "navigation" this is expected to return the complete object with a "navigation" label.
[
{
label: "Navigation",
accordion: true,
key: "navigation",
actions: [
{
action: "navigateTo",
label: "Navigate To"
},
{
action: "Back",
label: "Back"
},
{
action: "closeDialog",
label: "Close Dialog"
},
{
action: "delay",
label: "Delay"
}
]
}]
I need help building a search function which can be able to search the results on the basis of labels in the nested deep hierarchy. Here I want to perform a search on the basis of the "label" field. And will always return the result with its parent-wrapped object.
Here you can check the sample I have created:: https://codesandbox.io/s/bold-chihiro-4uv3dz?file=/src/App.js
2
Answers
You can write the search function like this:
Here is the working example:
https://codesandbox.io/embed/upbeat-tharp-if17ff?fontsize=14&hidenavigation=1&theme=dark
By the way now the search is case sensitive, you can change the
indexOf
with your desired text camparison.Here i have created this in plain JS. You can modify it according to your taste.
codesandbox React: https://codesandbox.io/s/withered-glitter-njg0mn