I think it should be easy but I couldn't figure out a decent solution to it. Here is a demo which illustrates what is my requirement: https://stackblitz.com/edit/angular-zjxvuh
I'm getting a response from the server like this:
[{"userId":10,"id":197,"title":"dignissimos quo nobis earum saepe","completed":true},{"userId":10,"id":198,"title":"quis eius est sint explicabo","completed":true},{"userId":10,"id":199,"title":"numquam repellendus a magnam","completed":true}]
And my typescript class looks like:
export class PostData {
public id?: number;
public title?: number;
}
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { PostData } from './post';
import { map, tap } from "rxjs/operators";
import { Observable } from 'rxjs';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular 4';
postData: PostData;
constructor(private http: HttpClient) { }
ngOnInit(): void {
var data = this.getFoo().subscribe(data => {
console.log("data",JSON.stringify(data))
});
//console.log(data);
}
getFoo(): Observable<PostData[]> {
return this.http.get<PostData[]> ('https://jsonplaceholder.typicode.com/todos')
.pipe(map(res => {
return res as PostData[];
})
)
}
}
I want server response in Post type array I mean I don't want that extra fields that coming from the server. How do I get rid of those extra fields?