Martheli Martheli - 2 months ago 30
Swift Question

Swift array to array of tuples

I have the following two arrays:

let xaxis = [monday, tuesday, wednesday, thursday, friday]
let yaxis = [1, 2, 3, 4, 5,]


I would like to merge them into an array that looks like this:

[ ("monday", 1), ("tuesday", 2), ("wednesday", 3), ("thursday", 4), ("friday", 5), ("saturday", 6) ]

Rob Rob
Answer Source

Use zip and map:

let xaxis = ["monday", "tuesday", "wednesday", "thursday", "friday"]
let yaxis = [1, 2, 3, 4, 5]

let tuples = zip(xaxis, yaxis).map { ($0, $1) }