Reputation: 173
How to store multiple json objects in to an array in angular7?
I am trying to save array of json objects like below :
employees: any;
ngOnInit() {
this.getemployee().subscribe
data => this.employees == data,
);
}
below is the response i am getting from the backend :
(32) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {id: "59744", employee_name: "Sample API ", employee_salary: "18000", employee_age: "20", profile_image: ""}
1: {id: "59747", employee_name: "Test", employee_salary: "123", employee_age: "456", profile_image: ""}
2: {id: "59748", employee_name: "hello#amdon", employee_salary: "10000", employee_age: "35", profile_image: ""}
.......
Upvotes: 0
Views: 337
Reputation: 1793
Try this solution:
Component.ts:
employees: IEmployeeData[] = [];
constructor(){ }
ngOnInit() {
this.getemployee().subscribe((data: IEmployeeData) => {
if(data) this.employees = data;
});
}
interface IEmployeeData {
id:string
employee_name: string;
employee_salary: string;
employee_age: string;
profile_image:string;
};
Upvotes: 0
Reputation: 1544
create an array of objects of the following structure.
//response data structure
export class ResponseData {
id:string
employee_name: string;
employee_salary: string;
employee_age: string;
profile_image:string;
}
component.ts
export class Test implements OnInit {
responseData:ResponseData[]=[];
constructor(){ }
ngOnInit() {
this.getemployee().subscribe((data)=>{
if(data != null) {
console.log('response received');
this.responseData = data;
}
});
}
}
Upvotes: 1