0

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: ""}
.......

2 Answers 2

1

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;
       }
    });   
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
   };

Comments

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.