javascript - Angular 2: Iterating over an array of objects and printing "Yes" to the view ONCE if any of the iterated objects return "true" for the check -


i have situation in angular 2 app iterating on array of objects, , if "completed" property set false of objects, want print view 'yes', because there (at least) 1 object "completed" equal false -- meaning it's still active flag.

however, right now, template code have printing 'yes' view each time 1 of these objects "completed" property set false returns true. how can adjust code i'm getting "yes" printed once if 1 (or more) of objects in array being iterated on has "completed" property set true?

here's code:

<td *ngfor="let flag of service.flags">      <ng-template *ngif="flag?.completed === false">          <span class="standard-flag">yes</span>      </ng-template>  </td>  

i tried using ternary operator, gives me same result:

<td *ngfor="let flag of service.flags">      <ng-template *ngif="flag?.completed === false ? true : false"">          <span class="standard-flag">yes</span>      </ng-template>  </td>  

while handle logic in component, i'm thinking there must way, perhaps indexing, in view on occasion.

you don't need ngfor make work. ngfor repeats every element in array, that's why see bunch yes's

use array.some() returns true immediately if at least 1 of elements in array returns true, right tool job here

<td>     <span *ngif="service.flags.some(flag => flag.completed === false) === true">         yes     </span> </td>  

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -