为什么对象数组中的过滤器不适用于打字稿

Jet*_*mes 1 javascript typescript angular

我有这个方法:

  pesquisar(): void {
    console.log(this.razaoSocial);

    if (this.razaoSocial || this.cnpj) {
      this.empresaDataSource = EMPRESAS.filter(empresa => {
        empresa.razaoSocial.indexOf(this.razaoSocial) > -1
      });
    } else {
      this.empresaDataSource = EMPRESAS;
    }

    console.log(this.empresaDataSource);
  }
Run Code Online (Sandbox Code Playgroud)

razaoSocialcnpj是绑定变量ngModel我的阵列EMPRESA具有两个对象:

export const EMPRESAS:Empresa[]=[
    {id:1, razaoSocial:'Ciclo Cairu', cnpj:'12345678912345'},
    {id:2, razaoSocial:'Industria', cnpj:'789456123456132'}
];
Run Code Online (Sandbox Code Playgroud)

但我应用过滤器,例如:Indus 在html字段中,预计对象Industria已被过滤empresaDataSource,但不是.

控制台中的输出日志是:

> Indus
> []
Run Code Online (Sandbox Code Playgroud)

我的错误在哪里?

Phi*_*ter 5

你使用的箭头功能是错误的.如果你添加花括号,它就像一个函数体,所以你必须在其中添加返回.

this.empresaDataSource = EMPRESAS.filter(empresa => {
    return empresa.razaoSocial.indexOf(this.razaoSocial) > -1
});
Run Code Online (Sandbox Code Playgroud)

要么

this.empresaDataSource = EMPRESAS.filter(empresa => empresa.razaoSocial.indexOf(this.razaoSocial) > -1);
Run Code Online (Sandbox Code Playgroud)