0

Hey guys i'm new with Ionic and I've got lot of doubts. In my project, I have requests and each request has an array of proposals. But when I pull the list from database I just wanna show the proposes who are 'Winner' and their requests are 'Finished', You guys got it? I'll show the code.

Html`

 <ion-segment-button value="third" (click)="getProposal('Winner')">
    Aceitas
  </ion-segment-button>

TypeScript

getProposals(status: string) {

if (status === 'Winner') {
  this.requestService.getProposalsPartner(this.user.email, status).then( resposta => {
        let proposalsWinner = resposta;
        for (let i in proposalsWinner) {
          if (proposalsWinner[i].request.status != 'Finished') {
            this.proposals = resposta;
          }              
        }
    })

} else {
      this.requestService.getProposalsPartner(this.user.email, status).then( resposta => {
      this.proposals = resposta;
    })  
}

}

So I've tried to make a for and only put on the list the proposals that follow the pattern I referred to. I've tested and it isnt working the way I wanted to. What's wrong on my code?

Ygor Fraga
  • 143
  • 1
  • 1
  • 10

1 Answers1

1

At first, it's worth to mention that you shouldn't use for..in with arrays.

Why? Quoted from this answer:

The for...in syntax mentioned by others is for looping over an object's properties; since an Array in JavaScript is just an object with numeric property names (and an automatically-updated "length" property), you can theoretically loop over an Array with it. But the problem is that it doesn't restrict itself to the numeric property values (remember that even methods are actually just properties whose value is a closure), nor does it iterate over those in numeric order. Therefore, the for...in syntax should not be used for looping through Arrays.

See this question to see more answers related to.


That said, the main problem in your code is that you aren't really filtering the resposta data, you're just assigning resposta to this.proposals. To filter, you can use (of course) Array#filter, like this:

this.proposals = response.filter(res => res.request.status !== 'Finished');

Final code:

getProposals(status: string) {
  this.requestService.getProposalsPartner(this.user.email, status).then(response => {
    if (status === 'Winner') {
      this.proposals = response.filter(res => res.request.status !== 'Finished');
    } else {
      this.proposals = response;
    }
  });
}

Template:

<ng-container *ngIf="proposals">
  <div *ngFor="let proposal of proposals">
    {{ proposal | json }}
  </div>
</ng-container>
developer033
  • 24,267
  • 8
  • 82
  • 108
  • I understood and you could help me a lot with the explanation, but when I typed (res => res.request.status) a message popped up "parameter 'res' has an any type". I typed (res: any) but didnt work, what's wrong? – Ygor Fraga Jun 26 '17 at 14:38
  • @YgorFraga Strange.. try: `(res: Object)` – developer033 Jun 26 '17 at 14:50
  • It didn't work, that's weird. I also tried "this.proposals = response.filter(res: any => res.request.status !== 'Finished');" – Ygor Fraga Jun 26 '17 at 15:04
  • @YgorFraga well it seems like more warning showed by tslint that anything. Where is the error is being shown? Also, what's the error exactly? – developer033 Jun 26 '17 at 15:07
  • Is being shown on 'res', the firts one on 'res => res.request.status'. The exactly error: Parameter 'rest' has an any type. (parameter) res: any – Ygor Fraga Jun 26 '17 at 15:09
  • @YgorFraga `response.filter((res: any)...` should work. – developer033 Jun 26 '17 at 15:12
  • 1
    Right, I didn't put (), thanks for helping lad! Your tips were valuable. – Ygor Fraga Jun 26 '17 at 15:14