Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 嵌套阵列和对象的角度管道|递归管道_Javascript_Angular_Ionic Framework - Fatal编程技术网

Javascript 嵌套阵列和对象的角度管道|递归管道

Javascript 嵌套阵列和对象的角度管道|递归管道,javascript,angular,ionic-framework,Javascript,Angular,Ionic Framework,我有一个巨大的数组,里面有多个数组。我试图用一个有角度的管道来过滤它,但我只能过滤第一层 这里有一个 我需要筛选整个表并显示与搜索文本匹配的结果您的搜索算法看起来不错(与项目名称匹配),但您没有搜索集合中最深的元素。要做到这一点,您只需将“用户”列表转换为您想要搜索的内容。如果您将来要实现类似的功能,那么利用TypeScript类型可以帮助您确保提取正确的内容。下面是一个写得很差的实现,它似乎适合您的案例 @Pipe({ name: 'myfilter' }) export class MyFi

我有一个巨大的数组,里面有多个数组。我试图用一个有角度的管道来过滤它,但我只能过滤第一层

这里有一个


我需要筛选整个表并显示与搜索文本匹配的结果

您的搜索算法看起来不错(与项目名称匹配),但您没有搜索集合中最深的元素。要做到这一点,您只需将“用户”列表转换为您想要搜索的内容。如果您将来要实现类似的功能,那么利用TypeScript类型可以帮助您确保提取正确的内容。下面是一个写得很差的实现,它似乎适合您的案例

@Pipe({ name: 'myfilter' })
export class MyFilterPipe implements PipeTransform {
  /**
   * In the future, replacing `any[]` with a type that actually
   * reflects the shape of your data will help you navigate
   * each of the properties.
   */
  transform(users: any[], args): any {
    return users.reduce((coll, top) => {
      // these nested reducers are bad, please don't
      // just copy and paste into your project
      if (top.subItemsList.length > 0) {
        return coll.concat(top.subItemsList.reduce((subColl, sub) => {
          if (sub.items.length > 0) {
            return subColl.concat(sub.items);
          }
          return subColl;
        }, []));
      }
      return coll;
    }, []).filter(user => user.itemName.toLowerCase().includes(args.toLowerCase()));
  }
}

演示。

在本例中,您只有一个级别。您说您有一个嵌套数组的数组,但在本例中,它只是一个对象数组,这就是您要键入的吗?从你的链接我得到了我期待的行为。。。你能解释一下你只过滤第一层是什么意思吗?对不起。我更新了示例。
@Pipe({ name: 'myfilter' })
export class MyFilterPipe implements PipeTransform {
  /**
   * In the future, replacing `any[]` with a type that actually
   * reflects the shape of your data will help you navigate
   * each of the properties.
   */
  transform(users: any[], args): any {
    return users.reduce((coll, top) => {
      // these nested reducers are bad, please don't
      // just copy and paste into your project
      if (top.subItemsList.length > 0) {
        return coll.concat(top.subItemsList.reduce((subColl, sub) => {
          if (sub.items.length > 0) {
            return subColl.concat(sub.items);
          }
          return subColl;
        }, []));
      }
      return coll;
    }, []).filter(user => user.itemName.toLowerCase().includes(args.toLowerCase()));
  }
}