Filter a javascript array dynamically using for

Asked

Viewed 62 times

0

I need to build a filter on a list, it turns out I’m having a problem because comparatives are dynamic. Basically I need to give the user the condition of filtering by, several payment conditions for example: 'check' and 'cash', then returns me everything that has check and cash.

To mount the filter options I simply use a push, where I create the object called filterRealized, and assemble the array within this object, the array would be matching:

this.filtrosRealizados.condicaoPgto.push(condicaoPgto);

After that on the screen where the filters will be run, I use a for to go through, getting the previously created object by parameter:

 modalEscolherFiltros.onWillDismiss((data => {
  if (data != undefined || data != null) {
    let condicoesPgto: any;
    condicoesPgto = data.condicaoPgto;
    console.log(this.financeiroRealizado);

    for (let i = 0; i < this.financeiroRealizado.length; i++) {
      this.financeiroRealizado = this.financeiroRealizado.filter((result: any) =>
        result.condicaoPagamento == condicoesPgto[i]
      );
    }
    console.log(this.financeiroRealizado);
  } else {
    console.log('nao tem parametro');
  }
}))

Then I filter my matrix, and I say that I want result.condicaoPagamento (which are the payment conditions I have in the matrix) equals the payment conditions I received per parameter. The result I hope is that return me the list with the payment terms that Filtrei, however I am receiving an empty list.

Updating: In the console.log I can’t get the two filters chosen on the previous screen, when I do console.log in the variable condicoesPgto, it returns me only the first payment condition, so I believe that’s where the problem is, but I don’t understand why of this.

inserir a descrição da imagem aqui

  • Doing a simple test, instead of calling Gto[i] conditions, I called 'Money' and returns me as expected, but I would like it to be dynamic

  • If putting the direct filter worked, how are the data of the variable condicoesPgto? Are they correct? By placing a console.log on it, you have the values you expected?

  • No, it returns only the first payment condition. I added an image with the console.log. In this case it does not return the condition of debit payment, however it should still work with money I think.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.