I am trying to write a search function that can search through an array of arrays of objects and return any objects that include the search string in the name.
The issue I am having is that the object array can contain an array of children and the children can also have an array of children. I need to dynamically search through all the possible children and return the results
I have tried to do this with Algolia but because the file structure won't be constantly updated I feel this would be better using Array.includes or something similar
I have tried the following function but can't seem to get it working
searchArray(subMenuItems, name) {
if (subMenuItems) {
for (let i = 0; i < subMenuItems.length; i++) {
if (subMenuItems.includes(name)) {
return subMenuItems[i];
}
const found = this.getSubItem(subMenuItems[i].children, name);
if (found) {
return found;
}
}
}
}
Here is an example of the Object Array
[
[
{
"children":[
{
"children":[
{
"fileSize":"1.2MB",
"fileUrl":"https://linktoPDF.com",
"name":"GF Kitchen ",
"type":"file"
}
],
"name":"Ground Floor Kitchen",
"type":"folder"
}
],
"name":"House",
"type":"folder"
}
],
[
{
"fileSize":"1.3MB",
"fileUrl":"https://linktoPDF.com",
"name":"Introduction and Overview",
"type":"file"
},
{
"fileSize":"20MB",
"fileUrl":"https://linktoPDF.com",
"name":"VISUAL iPad Location Drawing",
"type":"file"
},
{
"fileSize":"1MB",
"fileUrl":"https://linktoPDF.com",
"name":"Control Surface",
"type":"file"
},
{
"fileSize":"1.3MB",
"fileUrl":"https://linktoPDF.com",
"name":"Scene",
"type":"file"
}
]
]