I have the following JSON definitions:
export class Company {
name: string;
trips : Trip[] = [];
}
export class Trip{
id: number;
name: string;
}
I am able to see the trips in the console using:
console.log(this.company);
In the component I have the following method:
if(this.company) {
Object.keys(this.company.trips).forEach((data) => {
console.log(data);
});
}
What I am getting in the console is the trip's properties names which is "id" and "number". I would like to know how to access the value.
Object.keys?this.company.trips.forEach(trip => { console.log('trip.id', trip.id })"0","1", etc.), not things like"id"and"number".instanceof) and not get what you expect.Object.keysgives you the names of the own, enumerable, String-named properties of the object. If you want the values, you can useObject.valueswhich returns an array of the values of those properties. If you want both, you can useObject.entrieswhich returns an array of[key, value]arrays. Normally, though, you know the shape of the object and use its properties directly.