0

I have 2 arrays (diagnoses and labValues). I would like to loop through both arrays with only one element (swiping cards). Every array has a complete different structure, therefore I try to use ngIf.

With my current approach I do not get any labValues and ngIf also do not work. This approach feels ugly. Are there any easier approaches to do this.

  <div class="card-container">
    <sc-card padding *ngFor="let diagnose of diagnoses; let labValue of labValues"
    [orientation]="cardDirection"
    [tinder-card]="cardOverlay"
    [callDestroy]="diagnose.destroyEvent"
    [callLike]="diagnose.likeEvent"
    (onLike)="onCardInteract($event)">
    <div class="card-header">
      <ion-icon *ngIf="diagnose" class="clusterIcon" name="medkit"></ion-icon>
      <ion-icon *ngIf="labValue" class="clusterIcon" name="ios-flask"></ion-icon>
      <ion-badge *ngIf="diagnose" class="icdBadge">ICD: {{diagnose.additionalParameters.uniqueId}}</ion-badge>
      <ion-badge *ngIf="labValue" class="icdBadge" color="{{labValue.additionalParameters.interpretation}}">{{labValue.additionalParameters.interpretation | translate }}</ion-badge>
      <span *ngIf="diagnose" class="coveredText">'{{diagnose.coveredText}}'</span>
      <span *ngIf="labValue" class="coveredText">'{{labValue.coveredText}}'</span>
    </div>
    <div class="card-content">
      <span *ngIf="diagnose.additionalParameters.dictCanon" class="sectionHeader">{{"GeneralDiagnosis" | translate }}</span>
      <span *ngIf="diagnose.additionalParameters.dictCanon">{{diagnose.additionalParameters.dictCanon}}</span>

      <span *ngIf="labValue" class="sectionHeader">{{"GeneralLabValue" | translate }}</span>
      <span *ngIf="labValue">{{labValue.additionalParameters.parameter.additionalParameters.dictCanon}}</span>

      <span class="sectionHeader">{{"Explanation" | translate }}</span>
    </div>
    <div class="card-footer">
      <span class="swipeText">{{"swipeResult" | translate }}</span>
      <ion-icon class="swipeLeft" name="ios-thumbs-down"></ion-icon>
      <ion-icon class="swipeRight" name="ios-thumbs-up"></ion-icon>
    </div>
     </sc-card>
  </div>
4
  • does these two arrays have any common properties between them? e.g. is each diagnose supposed to have a lab value? if yes, i would probably merge the labValue to their respective diagnose item. Commented Jul 4, 2018 at 12:18
  • You cant loop two arrays at same time in an *ngFor.. Commented Jul 4, 2018 at 12:23
  • Combine the two arrays into a single array in your angular data structure code. Commented Jul 4, 2018 at 12:25
  • The arrays are completely different Commented Jul 4, 2018 at 12:59

2 Answers 2

2

So you have 2 arrays containing 2 slightly different objects.

I would create a new array for your cards containing (as much as possible) a standard object :

let cardArray = diagnoses.map(diag => { type: 'diagnose', 
          ICD: diag.additionalParameters.uniqueId,
          coveredText: diag.coveredText }) // etc... map all your properties
     .concat(labValues.map(labVal => { type: 'labvalue',
          interpretation: labVal.additionalParameters.interpretation,
          coveredText: labVal.coveredText })) // etc... map all your properties

Now you can just loop over your new cardArray array and make your HTML clearer.

Sign up to request clarification or add additional context in comments.

Comments

0

you could create an auxiliary array :

  let auxArray = [];
  for (let i; i <= Math.max(diagnoses.length); labValues.length, i++) {
     auxArray.push( {"index": i} );
  }

Then in your *ngFor, iterate the auxArray, and with the index, show the corresponding diagnoses or labValues ( check for undefined to alleviate array.length differences )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.