0

I have a video array which is an element in an object of a response array . Now i want to return only a single video for each response title i.e , Each match should only have one highlight video.

Here's my endpoint

    "response": [
        {
            "title": "Chelsea - Brighton",
            "competition": "ENGLAND: Premier League",
            "videos": [
                {
                    "title": "Highlights",
                    "embed": "<div> A video 1<\/div>"
                },
                {
                    "title": "Highlights",
                    "embed": "<div> A video 2 <\/div>"
                }
            ]
        },
        {
            "title": "Brentford - Manchester City",
            "competition": "ENGLAND: Premier League",
           
            "videos": [
                {
                    "title": "Highlights",
                    "embed": "<div> A video  <\/div>"
                }
            ]
        }
}

My template


         <div class="panel">
      
            <div *ngFor="let video of result.videos">
              <ng-container *ngIf="video.title == 'Highlights'">
                <p [innerHTML]="video.embed | safe"></p>
                <p>{{out.title}}</p> 
              </ng-container>
            </div>
     </div>
     </div>```

I've tried ```<div *ngFor="let video of result.videos[0]"></div>``` But this gives only a single team result

1 Answer 1

2

You can create a pipe to filter the videos based on the title:

@Pipe({name: 'filterVideos'})
export class FilterVideosPipe implements PipeTransform {
  transform(videos: any[], title: string): any {
    return videos.filter(video => video.title === title);
  }
}

and then use it as:

<div *ngFor="let video of result.videos | filterVideos:'Highlights'; let index=index">
    <ng-container *ngIf="index === 0">
        <p [innerHTML]="video.embed | safe"></p>
        <p>{{out.title}}</p> 
    </ng-container>
</div>

You can always enhance the Pipe transform method as per your requirements and make it generic.

Creating pipes for custom data transformations

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

1 Comment

This did the job bro , thanks !

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.