Angular Can';t在管道中的对象数组上不使用.filter()或

Angular Can';t在管道中的对象数组上不使用.filter()或,angular,typescript,Angular,Typescript,我正在创建的自定义管道有问题。我的Perso是一个Perso对象数组,看起来我无法应用.filer(),所以我尝试了一个简单的for循环(在这样的Perso变量上的*ngFor中工作得很好),但也没有成功。我想我缺少了一些关于在Typescript中处理对象的基本知识 以下是管道代码以及一些测试和注释: import { Pipe, PipeTransform } from '@angular/core'; import { Perso } from './perso'; @Pipe({ n

我正在创建的自定义管道有问题。我的Perso是一个Perso对象数组,看起来我无法应用.filer(),所以我尝试了一个简单的for循环(在这样的Perso变量上的*ngFor中工作得很好),但也没有成功。我想我缺少了一些关于在Typescript中处理对象的基本知识

以下是管道代码以及一些测试和注释:

import { Pipe, PipeTransform } from '@angular/core';

import { Perso } from './perso';

@Pipe({ name: 'startsWithPersoPipe' })
export class StartsWithPersoPipe implements PipeTransform {
  transform(Persos: Perso[]){

    // this for makes it all bug, when I comment it my pipe works fine
    for( let perso of Persos){
    }

    // this for does not make it crash but does not behave at all like I want
    for( let perso in Persos){
      console.log(perso); // just prints 0, 1, 2, 3 etc up to 109 ( i have 110 elements in the Perso[] var I use for test)
      console.debug(perso); // same behavior as console.log(perso)
      console.log(perso.nom); // undefined
    }
    console.debug(Persos); // Array [ Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, 100 more… ]
                          // when I click on on of the Object I get one of my objects with correct values in nom, description and type : it's all fine !
    return Persos;
  }
}
这是个人

export class Perso {
  nom: string;
  type: string;
  description: string;
}
非常感谢任何有助于我解决此问题的帮助、提示或有用资源的链接。

这应该可以:

@Pipe({ name: 'startsWithPersoPipe' })
export class StartsWithPersoPipe implements PipeTransform {
  transform(Persos: Perso[]){
    if(Persos == null) {
      return null;
    }

    return persos.filter(p => p.nom && p.nom.startsWith('super'));
  }
}

“不能使用.filter()”的确切含义是什么?期望的行为是什么?它就像for of,使它崩溃。最终,我想过滤掉所有Perso对象,这些对象的“nom”以“Super”开头,但这部分并不是它的缺陷所在,所以我删除了所有不必要的东西,只在这里显示错误的代码。谢谢!这让我很困惑,因为他们不这么做(cf app/flying-heroses.pipe.ts)。我应该什么时候?为什么他们不需要它,而我需要呢?我想他们只是不传入
null
;-)。当在
null
上调用
.filter(…)
时,您应该会收到一条有意义的错误消息。正如您在console.debug Persos中看到的,它不是空值。此外,我刚刚将if(Persos==null){returnnull;}添加到我的代码中,其余部分工作正常,这意味着Persos不是null,但出于某种原因,需要进行验证。也许我的管道函数是在angular将其值赋予Persos之前浇铸的,然后Persos被初始化并再次应用管道。我猜它首先是用
null
调用的,然后用非null值再次设置该值。只检查
null
没有任何作用。