将数组AB中的元素追加到数组A中的每个元素

时间:2019-10-18 04:04:41

标签: swift

我有两个数组,我想将数组B的元素(时间)附加到数组A的每个元素(日期)中,以创建一个新的数组

[String:[String]]

我希望输出为

[“ 10/20/19”:[“ 8:00 AM-10:00 AM”,“ 10:00 AM -12:00 PM”,“ 12:00 PM-2:00 PM”], “ 10/21/19”:[“ 8:00 AM-10:00 AM”,“ 10:00 AM -12:00 PM”,“ 12:00 PM-2:00 PM”]]

import UIKit

var deliveryDates: [String] = []
var deliveryTimes: [String] = []

deliveryDates = ["10/20/19","10/21/19"]
deliveryTimes = ["8:00 AM - 10:00 AM","10:00 AM -12:00 PM","12:00 PM - 2:00 PM"]

var deliveryWindows: [String: [String]] = [:]


func createDeliverySlots(dDates: [String], dtimes: [String]){

    var ddeliveryWindows: [String: [String]] = [:]

    let ddtimes:[String] = dtimes
    var dddates:[String] = dDates

    for _ in dddates{
     dddates.append("\(ddtimes)")

    }

   print(dddates)


}

createDeliverySlots(dDates: deliveryDates, dtimes: deliveryTimes)

1 个答案:

答案 0 :(得分:0)

您可以将map数组与deliveryDates数组一起deliveryTimes,然后将其缩小为所需的结果:

var deliveryWindows: [String: [String]] = [:]
deliveryWindows = deliveryDates.map({ [$0: deliveryTimes]}).flatMap({ $0 }).reduce([:], { ( dict, tuple) in
    var tempDict = dict
    tempDict[tuple.0] = tuple.1
    return tempDict
})