Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}

>Solution :

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’)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading