What is the best way to align an array of dates with another array.

I'm racking my head here trying to figure out the best way to do this, but I have two arrays. One array is a range of dates and another is a return of data.

Here's the function that returns an array of dates

func numberOfDaysBetween(startDate: Date, raceDate: Date) -> [Date] {
  let numberOfDays = Calendar.current.dateComponents([.day], from: startDate, to: raceDate).day!
  let week = (-numberOfDays...0).compactMap {
   Calendar.current.date(byAdding: .day, value: $0, to: raceDate)
  }
  return week
}

I'm trying to get the dates to align to the weekdays listed here. https://api.npoint.io/6a55ea69409d7f3d90b4.

Would a ZStack be appropriate here?

Answered by robevans in 688297022

I figured it out.

Using the below worked

           ForEach(planData.trainingdata.prefix(1)) { index in
            ForEach(dateRange, id: \.self) { day in

Could you show what is the expected result ? DO you want to move 09/08 to the right ? And post the present code.

Correct. Ideally 09/08, 09/09 would match with the appropriate day. Here's a pretty bare bones of the code. Sorry I can't get it formatted right

@StateObject var planData = DownloadHansonData() @AppStorage("startDate") var userStartDate: Date = Date() @AppStorage("raceDate") var userRaceDate: Date = Date() @State var startDate = Date() @State var endDate = Date() @State var dateRange: [Date] = [] var body: some View { VStack { // ForEach(dateRange, id: \.self) { day in ScrollView { ForEach(planData.trainingdata) { index in HStack { Text("\(index.day0.day) - \(index.day0.exercise)") Spacer() } HStack { Text("\(index.day1.day) - \(index.day1.exercise)") Spacer() } HStack { Text("\(index.day2.day) - \(index.day2.exercise)") Spacer() } HStack { Text("\(index.day3.day) - \(index.day3.exercise)") Spacer() } HStack { Text("\(index.day4.day) - \(index.day4.exercise)") Spacer() } HStack { Text("\(index.day5.day) - \(index.day5.exercise)") Spacer() } HStack { Text("\(index.day6.day) - \(index.day6.exercise)") Spacer() } // } } .padding() } } }

I think I figured it out sort of, this though iterates through daterange and then planData

           ForEach(planData.trainingdata) { index in             ForEach(dateRange, id: \.self) { day in
Accepted Answer

I figured it out.

Using the below worked

           ForEach(planData.trainingdata.prefix(1)) { index in
            ForEach(dateRange, id: \.self) { day in
What is the best way to align an array of dates with another array.
 
 
Q