I have a Data which am getting from API, its in a format
[
Appointments(startTime:"2022-09-01",subject:"Clinic Appointment",activityid:"2343"),
Appointments(startTime:"2022-09-01",subject:"Night Appointment",activityid:"5738"),
Appointments(startTime:"2022-09-01",subject:"Regular checkup",activityid:"2975"),
Appointments(startTime:"2022-09-02",subject:"High Fever",activityid:"9274"),
Appointments(startTime:"2022-09-02",subject:"Virtual checkup",activityid:"5648"),
Appointments(startTime:"2022-09-03",subject:"Regular Appointment",activityid:"1892")
]
and I have an array where I have all the Dates of this month,
what I want is I want the data to be spread across my month with respect to date in the month
ex:
[
AppointmentsByDates(title:"2022-09-01" , appointments: [
Appointments(startTime:"2022-09-01",subject:"Night Appointment",activityid:"5738"),
Appointments(startTime:"2022-09-01",subject:"Regular checkup",activityid:"2975"),
Appointments(startTime:"2022-09-02",subject:"High Fever",activityid:"9274"),
]),
AppointmentsByDates(title:"2022-09-02" , appointments: [
Appointments(startTime:"2022-09-02",subject:"High Fever",activityid:"9274"),
Appointments(startTime:"2022-09-02",subject:"Virtual checkup",activityid:"5648"),
]),
AppointmentsByDates(title:"2022-09-03" , appointments: [
Appointments(startTime:"2022-09-03",subject:"Regular Appointment",activityid:"1892")
]),
AppointmentsByDates(title:"2022-09-04" , appointments: []),
AppointmentsByDates(title:"2022-09-05" , appointments: []),
AppointmentsByDates(title:"2022-09-06" , appointments: []),
.
.
.
AppointmentsByDates(title:"2022-09-30" , appointments: []),
]
If you see in the above example startTime is what the Data is segregate with,
I did this in React Native using Maps and Reduce in Javascript, am new to iOS i need this structured data to populate tableview with multiple sections of every Date in a month.
This image represents what I actually want in tableView
Can you help me out with it?
Thanks,
2
Answers
Try this reduce method:
First of all, assume you have two struct which is Appointments and AppointmentsByDates
And you have your array just like above
And for your question, It can divide into two task: you can map your array into dictionary which key is startTime and the value is list of Appointments which map which that key ; The keys here are the title of AppointmentsByDates
And then you will have your result