I have a series of time
entries within a data
JSON array that I need to parse and format using Moment.js. The formatted data will then be stored in the this.appointmentTimes
array for easy access on my view using {{appointmentTime.time}}
.
Here is the JSON structure:
[
{ "time":"2018-10-22T14:30:00+0200", "slotsAvailable":1 },
{ "time":"2018-10-22T14:45:00+0200", "slotsAvailable":1 },
{ "time":"2018-10-22T15:00:00+0200", "slotsAvailable":1 }
]
Upon refreshing the page, the following error occurs:
ERROR in src/app/pages/booking/form/booking.component.ts(75,9): error TS2322: Type 'string' is not assignable to type 'object[]'.
src/app/pages/booking/form/booking.component.ts(76,22): error TS2345: Argument of type 'object[]' is not assignable to parameter of type 'string'.
This function is called from my view and handles the corresponding logic:
private appointmentLocations: Array<object> = [];
private appointmentTypes: Array<object> = [];
private appointmentTimes: Array<object> = [];
onChangeTypeId(TypeId) {
console.log(TypeId);
this.selectedAppointmentTypeId = TypeId;
this.apiService
.getAppointmentTimesById(
this.selectedAppointmentTypeId,
this.selectedAppointmentLocation,
this.selectedDatePicker
)
.subscribe((data: Array<object>) => {
/*this.appointmentTimes = data; */
this.appointmentTimes = JSON.stringify(
JSON.parse(data).map(function(data) {
var pattern = 'HH:mm';
data = moment(data.time).format(pattern);
return data;
})
);
console.log(data);
});
}
}