javascript - 编写递归搜索函数以返回对象;

标签 javascript arrays angular google-cloud-firestore

我正在尝试编写一个搜索函数,它可以搜索对象数组的数组并返回名称中包含搜索字符串的任何对象。

我遇到的问题是对象数组可以包含子数组,并且子数组也可以有子数组。我需要动态搜索所有可能的子级并返回结果

我尝试使用 Algolia 来做到这一点,但由于文件结构不会不断更新,我觉得使用 Array.includes 或类似的东西会更好

我尝试了以下功能,但似乎无法使其工作

  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;
        }
      }
    }
  }

这是对象数组的示例

[
   [
      {
         "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"
      }
   ]
]

最佳答案

一个简单的递归函数将允许您从每个嵌套(无论多深)对象中收集对象(或对象的任何属性)。

您应该考虑区分大小写是否也很重要。

否则,这将起作用:

  • 在数据中搜索任何带有“ce”的名称
  • 然后搜索任何带有“tion”的名称
  • 然后搜索任何包含“Floor”的名称

const data = [[{"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"}]];
let output = [];

function search(arr, str) {
  arr.forEach(a => {
    if (a.constructor == Array) {
      search(a, str);
    } else if (a.children) {
      if (a.name.includes(str)) output.push(a);
      search(a.children, str);
    } else {
      if (a.name.includes(str)) output.push(a);
    }

  });
}
search(data, 'ce');
console.log(output);
output = [];
search(data, 'tion');
console.log(output);
output = [];
search(data, 'Floor');
console.log(output);

关于javascript - 编写递归搜索函数以返回对象;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56712579/

相关文章:

javascript - 使用 FCM firebase node js express 发送批量推送通知

javascript - 如果屏幕尺寸小于,则停止脚本工作

javascript - 在 JavaScript 中使用数组作为字典的键

angular - 如何配置一个在大屏幕和小屏幕上都很好用的 Material cdk 覆盖位置策略?

angular - 当它不是第一个选项卡时,@ViewChild 在 mat-tab 中未定义

javascript - 什么是 ngModel.$validators 管道?

javascript - 在 Javascript 中创建一个吸引人的字符串

php - usort 按连续值对多维数组进行排序

arrays - 有效地合并两个数组 - 一个已排序,另一个未排序

使用 [routerLink] 时出现 Angular "Error: Cannot match any routes. URL Segment",但它适用于 this.router.navigate