I have received data from a web API that resembles the structure below. I am looking for guidance on how to properly map the product array into individual Products. My main objective is to convert the eating_time values into JavaScript datetime format.
Currently, my code snippet where this.products = product simply saves the array along with the list of products.
//Data retrieved from the API
[ {datetime: "2017-06-25T07:45:00+08:00", name: "Apple", unit: 1, product: [{eating_time: "2017-06-25T07:45:00+08:00", qtytaken: 1, chart_bar=1 },
{eating_time: "2017-06-25T17:45:00+08:00", qtytaken: 1, chart_bar=1 },
{eating_time: "2017-06-25T23:55:00+08:00", qtytaken: 1, chart_bar=1 } ]
]
// Product Class
class Product {
eating_time: Date;
qtytaken: number;
chart_bar = 1;
constructor({eating_time, qtytaken, chart_bar }) {
this.eating_time = new Date(eating_time);
this.qtytaken = dosage;
this.chart_bar = chart_bar;
}
}
export class ProductTaken {
datetime: Date;
name: string;
unit: number;
products: Product[];
constructor({datetime, name, unit, product} ) {
this.datetime = new Date(datetime) ;
this.name = name;
this.unit = unit;
this.products = product ;
}
}