Jeterson Miranda Gomes
Jeterson Miranda Gomes

Reputation: 5483

Why the filter in object array not works in typescript

i have this metod:

  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);
  }

the razaoSocial and cnpj is a bind variable ngModel My array EMPRESA have two objects:

export const EMPRESAS:Empresa[]=[
    {id:1, razaoSocial:'Ciclo Cairu', cnpj:'12345678912345'},
    {id:2, razaoSocial:'Industria', cnpj:'789456123456132'}
];

But i apply the filter, for example: Indus in the field of html, was expected the object Industria has been filtered in empresaDataSource, but not.

the output log in console is:

> Indus
> []

Where is it my error?

Upvotes: 1

Views: 51

Answers (1)

Phiter
Phiter

Reputation: 14992

You're using arrow function the wrong way. If you add curly braces, it becomes like a function body, so you have to add return inside of it.

this.empresaDataSource = EMPRESAS.filter(empresa => {
    return empresa.razaoSocial.indexOf(this.razaoSocial) > -1
});

or

this.empresaDataSource = EMPRESAS.filter(empresa => empresa.razaoSocial.indexOf(this.razaoSocial) > -1);

Upvotes: 5

Related Questions