I am trying to retrieve data from a dummy url and map the data into a list. To test the data, I want to display them in an alert/Loop. I am using this dummy api url: http://dummy.restapiexample.com/api/v1/employees
It returns data in this format:
{"status":"success","data":[{"id":"1","employee_name":"Tiger
Nixon","employee_salary":"320800","employee_age":"61","profile_image":""},
{"id":"2","employee_name":"Garrett
Winters","employee_salary":"170750","employee_age":"63","profile_image":""} ...
Below is my typescript code:
constructor(private http: HttpClient) { }
configUrl = 'http://dummy.restapiexample.com/api/v1/employees';
getEmployees() {
var x = this.http.get<Employee[]>('http://dummy.restapiexample.com/api/v1/employees').pipe(map(res =>
res['data']));
alert(JSON.stringify(x));
}
This is my class Employee:
export class Employee {
id: any;
employee_name: any;
employee_salary: any;
employee_age: any;
profile_image: any;
}
Based on my research, we should use the pipe(map(res =>res['data']), but it is not working for me. Anyone can help understand what is wrong with the above code please?