简体   繁体   English

过滤多面阵列,但管道返回错误 - 找不到'对象'类型的不同支持对象'[object Object]'

[英]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.

相关问题 找不到&#39;对象&#39;类型的不同支持对象&#39;[object Object]&#39; - Cannot find a differ supporting object '[object Object]' of type 'object' 找不到其他支持对象&#39;[对象对象] - Cannot find a differ supporting object '[object Object] Angular:*ngFor 循环给我错误:找不到类型为“object”的不同支持对象“[object Object]” - Angular: *ngFor loop giving me error: Cannot find a differ supporting object '[object Object]' of type 'object' 角度4无法找到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到可迭代对象,例如数组 - Angular 4 Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays Angular 9 找不到支持“对象”类型的 object“[对象对象]”的不同。 NgFor 仅支持绑定到诸如 Arrays 之类的 Iterables - Angular 9 Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays 错误:找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到Iterables(如数组)如何解决 - error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays how to solve 找不到类型为“object”的不同支持对象“[object Object]”。 - 上下文菜单问题 - Cannot find a differ supporting object '[object Object]' of type 'object'. - context menu issues 找不到类型为“数字”的不同支持对象“33.265625” - Cannot find a differ supporting object '33.265625' of type 'number' 在 Angular 上执行 GET 请求时出现此错误:找不到不同的支持 object '[object Object] - Getting this error when doing a GET request on Angular: Cannot find a differ supporting object '[object Object] Angular 问题:找不到支持“object”类型的 object“[object Object]”的不同。 NgFor 仅支持绑定到诸如 Arrays 之类的 Iterables - Angular problem: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM