Ad

How To Get Data From An TypeScript Object?

- 1 answer

Block of code in modal:

export class Profile {
    constructor(public id : number,public name : string, public age: number,public address:any)
}

Block of code in TypeScript file:

profile : Profile[]= [new Profile(1,'hello',4,'address'),new Profile(1,'hello',4,'address'),
    new Profile(1,'hello',4,'address'),new Profile(1,'hello',4,'address')
  ];

How to get name id age as a list and navigate through JSON object array, because following block of code is not displaying anything as it is an array of objects in JSON:

<div *ngFor="let p of profile">
    <ion-button>{{p.id}}</ion-button>
</div>
Ad

Answer

For this particular problem, it is better to use interface :

interface Profile {
id : number;
name : string; 
age: number;
address:any;
}

now use it like so :

let profile : Profile[] = [{id : 1 , name : "Shahab" , age : 23 , adress : "Some where"} , ...] 

Now you can access it easily .

Ad
source: stackoverflow.com
Ad