As a beginner in Angular, I am attempting to filter an array by using modulus on the id when clicking a button. Despite researching online and trying to use pipe for this purpose, I keep encountering an error stating that "pipe" does not exist. I am unsure why this is happening. Using .filter() on its own does not work, and incorporating pipe does not solve the issue either. Am I heading in the wrong direction for a simple onclick filter? Below is the code I am working with:
Here is the code snippet:
import { Component, OnInit} from '@angular/core';
import { StreamService } from '../stream.service';
import { Stream } from '../stream';
import { map } from 'rxjs/operators';
@Component({
selector: 'app-discover',
templateUrl: './discover.component.html',
styleUrls: ['./discover.component.scss']
})
export class DiscoverComponent implements OnInit {
streams!: Stream[];
constructor(private streamService: StreamService) {
}
ngOnInit() {
this.getStreams();
}
getStreams(){
this.streamService.getStream().subscribe((data =>{
this.streams = data;
console.log(this.streams);
}))
}
filterIsUneven(){
this.streams.pipe(map((streams => streams.filter(stream => stream.id % 3)))
};
}
<div class="container">
<div class="buttons">
<button (click) = "filterIsUneven()"> Games </button>
<button> Music </button>
<button> Esports </button>
<button> IRL </button>
<button>Back</button>
</div>
<div class="streams" *ngFor="let stream of streams">
<h3>{{stream.id}}</h3>
<h3>{{stream.title}}</h3>
<img src="{{stream.thumbnailUrl}}">
</div>
</div>
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Stream } from './stream';
import { Observable} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class StreamService{
constructor(private http: HttpClient) { }
getStream():Observable<Stream[]>{
return this.http.get<Stream[]>("https://jsonplaceholder.typicode.com/albums/1/photos");
}
getLiveStream(id:number):Observable<Stream[]> {
const url = `https://jsonplaceholder.typicode.com/albums/1/photos?id=${id}`;
return this.http.get<Stream[]>(url);
}
}