skip to Main Content

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


  1. Try this reduce method:

    let sections = array.reduce(into: [AppointmentsByDates]()) { result, element in
        if let index = result.firstIndex(where: { resuty in
            return resuty.appointments.contains(where: {$0.startTime == element.startTime})
        }) {
            result[index].appointments.append(element)
        }else {
            result.append(AppointmentsByDates(title: element.startTime, appointments: [element]))
        }
    }
    
    Login or Signup to reply.
  2. First of all, assume you have two struct which is Appointments and AppointmentsByDates

    struct Appointments {
        var startTime : String
        var subject : String
        var activityid:  String
    }
    
    struct AppointmentsByDates {
        var title: String
        var appointments : [Appointments]
    }
    

    And you have your array just like above

    let arr = [
        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 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

    // make dictionary which key is starttime
    var dict : [String:[Appointments]] = [:]
    for value in arr {
        dict[value.startTime, default: []].append(value)
    }
    
    // make result mapping the key of dictionary to title
    var result : [AppointmentsByDates] = []
    for key in dict.keys {
        let value = AppointmentsByDates(title: key, appointments: dict[key]!)
        result.append(value)
    }
    

    And then you will have your result

    print("result: ", result) // get your result here
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search