Home > database >  How to use filter() with push() in the same function
How to use filter() with push() in the same function

Time:04-28

I would like to know if there would be a way to use the filter with the push in the same function, I tried here, but it didn't work

here is an example of the code

          this.storeS.getItensByCateg(paramId).subscribe((resB: any) => {
        scrollTo(0, 300);
        this.slicedItems = [];
        for (let i = 0; i <= 200; i  ) {
          if (resB[i]) {
            this.slicedItems.push(resB[i]);
          }
            console.log(this.slicedItems.filter(item => item.status==='I'))
          }

In console.log the filter works normally, but in push(resB[i]) it doesn't work

the desired exit

estoque: null fotos: (6) ['8407.0', null, null, null, null, null] id: 8407 item_linkWeb: {id: 719, emp_id: 1, item_id: 8407, linkWeb_id: 693, updated_at: null, …} status: "I" tabela: {itemTabela_id: 4, item_id: 8407, vVenda: 212.9, vPromo: null, vPromoData: null}

But the output could also come with the status :"A", so I need the filter status==='I'

estoque: null fotos: (6) ['8407.0', null, null, null, null, null] id: 8407 item_linkWeb: {id: 719, emp_id: 1, item_id: 8407, linkWeb_id: 693, updated_at: null, …} status: "A" tabela: {itemTabela_id: 4, item_id: 8407, vVenda: 212.9, vPromo: null, vPromoData: null}

CodePudding user response:

Because filter returns an array, not mutates the original.

That's why console log shows you, but it doesn't update.

You need

this.slicedItems = this.slicedItems.filter(item => item.status==='I')
  • Related