可以设置PipeTransform多个过滤器

时间:2017-03-18 15:23:42

标签: javascript angular typescript ionic-framework ionic2

可以设置PipeTransform多个过滤器吗?

我试试

 posts;
  postss;
    transform(items: any[]): any[] {
             if (items && items.length)
             this.posts = items.filter(it => it.library = it.library.replace("WH","Warehouse"));
             this.postss = items.filter(it => it.collection = it.collection.replace("CL","Central Library"));                                                        

             return this.posts,this.postss;
         }

这不起作用

posts; 
transform(items: any[]): any[] {
                 if (items && items.length)
                 this.posts = items.filter(it => it.library = it.library.replace("WH","Warehouse"));
return this.posts;
             }

这有效。

2 个答案:

答案 0 :(得分:1)

由于您需要更改filter数组中的每个项目,因此无需使用forEach,请改用 transform(items: any[]): any[] { if (items && items.length){ items.forEach((it,index,array) => { array[index].library = it.library.replace("WH","Warehouse"); array[index].collection = it.collection.replace("CL","Central Library"); }); } return items ; } 并应用更改:

.clear {
    clear: both;
}

答案 1 :(得分:0)

return this.posts.concat(this.postss);

来自concat的文档:

  

concat()方法返回一个新数组,该数组由调用它的数组组成,并与作为参数提供的数组和/或值连接。