1

Example:

I have the following Object:

{
  "Person": {
    "Name": {},
    "Hobbies": {
      "0": {
        "Description:Soccer": {},
        "IsActive:false": {}
      },
      "1": {
        "Description:Hockey": {},
        "IsActive:false": {}
      },
      "2": {
        "Description:Tennis": {},
        "IsActive:true": {}
      }
    }
  }
}

I would like to loop through the array and check wether any hobby is active. If so it should return true, and display an image.

<img [src]="Person.Hobbies != null ? './images/hobby.png' : ''"/>

I would like the image to only show one time.

Is that possible using html?

2 Answers 2

1

You can loop with ngFor, to show all in array.

<img [src]='./images/hobby.png' *ngFor="let hobby in Person.Hobbies"> </img >

then add an ngIf condition for each hobby in the list if there's any specific reason you only want to show a subset.

Example

<div *ngFor="let hobby in Person.Hobbies">
  <img *ngIf="hobby.IsActive"  [src]='./images/hobby.png'>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Exactly what I was looking for.
0

I would set a property in your Component

<img *ngIf="showHobbyImg">

this.showHobbyImg = false;

const hobbies = this.person.Person.Hobbies;
for (key in hobbies) {
  if (hobbies["IsActive:true"]) {
    this.showHobbyImg = true;
    break;
  }
}

If you're using lodash you could simplify this a bit:

this.showHobbyImg = _.some(this.person.Person.Hobbies, ['IsActive:true', {}]);

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.