Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zip arrays in Swift? [duplicate]

Tags:

arrays

swift

let array1 = ["Albert","Bobby"]
let array2 = ["Charles", "David"]

How do merge two array so that the out put would be ["Albert", "Charles", "Bobby", "David"]

like image 452
Muzahid Avatar asked Mar 28 '16 12:03

Muzahid


1 Answers

You can use zip to combine your two arrays, and thereafter apply a .flatMap to the tuple elements of the zip sequence:

let array1 = ["Albert","Bobby"]
let array2 = ["Charles", "David"]

let arrayMerged = zip(array1,array2).flatMap{ [$0.0, $0.1] }

print(arrayMerged) // ["Albert", "Charles", "Bobby", "David"]
like image 127
dfrib Avatar answered Oct 20 '22 22:10

dfrib