[英]Filtering a multifaceted array, but the pipe return an error - Cannot find a differ supporting object '[object Object]' of type 'object'
I am trying to filter output, but one field in the array is an array. 我试图过滤输出,但数组中的一个字段是一个数组。
When I'am filter all - I get all. 当我过滤所有 - 我得到了所有。 If I filter for any subset, the console.log(items[i].tags) output in array foramat ["Skills", "Learning"]
. 如果我过滤任何子集,则console.log(items [i] .tags)输出数组foramat ["Skills", "Learning"]
。 The 2nd console.log displays the item in the array (with both tags) 第二个console.log显示数组中的项目(包含两个标记)
The return throws the error: 返回抛出错误:
ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. at NgForOf.push../node_modules/@angular/common/fesm5/common.js.NgForOf.ngDoCheck
... in the HTML: ...在HTML中:
<span *ngFor="let page of page | tags: tag ; #nGForArray>
... The page array ...页面数组
[ {
"id" : "1",
"title" : "Security",
"tags" : ["Skills", "Learning"]
},
{
"id" : "2",
"title" : "Home Improvement",
"tags" : ["Skills"]
},
{
"id" : "3",
"title" : "Mathematics",
"tags" : [""Learning"]
}
]
... in search.pipe.ts ...在search.pipe.ts中
export class TagPipe implements PipeTransform {
transform(items: any[], tag: string): any {
if (!items || !tag) {
return items;
}
var filtertags: any[];
for (let i = 0; i < items.length; i++) {
console.log(items[i].tags);
for (let j = 0; j < items[i].tags.length; i++) {
if (tag == items[i].tags[j]) {
console.log(items[i]);
return items[i];
}
}
}
console.log("end of transform");
console.log(filtertags);
return filtertags;
}
}
I need the results to find any record where one of the tags matches the filter. 我需要结果来查找其中一个标签与过滤器匹配的任何记录。
I made some changes in your transform method, not changing your logic, and commented them below. 我在转换方法中做了一些更改,而不是更改逻辑,并在下面对它们进行了评论。
Also if you are modifying your items in execution time, you may need define your pipe as impure. 此外,如果您在执行时修改项目,则可能需要将管道定义为不纯。 Just add "pure: false" to your pipe, like @Pipe({ name: "blabla", pure: false }) 只需在管道中添加“pure:false”,如@Pipe({name:“blabla”,pure:false})
transform(items: any[], tag: string): any {
if (!items || !tag) {
return items;
}
//initialized it
var filtertags: any[] = [];
for (let i = 0; i < items.length; i++) {
console.log(items[i].tags);
//it was increasing i instead of j
for (let j = 0; j < items[i].tags.length; j++) {
if (tag == items[i].tags[j]) {
console.log(items[i]);
//instead of returning, push to the filtertags array
filtertags.push(items[i]);
}
}
}
console.log("end of transform");
console.log(filtertags);
return filtertags;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.