This question already has an answer here:
这个问题在这里已有答案:
- How can I interleave two arrays? 2 answers
- 如何交错两个数组? 2个答案
let array1 = ["Albert","Bobby"]
let array2 = ["Charles", "David"]
How do merge two array so that the out put would be ["Albert", "Charles", "Bobby", "David"]
如何合并两个数组,以便输出将是[“阿尔伯特”,“查尔斯”,“鲍比”,“大卫”]
2 个解决方案
#1
12
You can use zip
to combine your two arrays, and thereafter apply a .flatMap
to the tuple elements of the zip sequence:
您可以使用zip来组合两个数组,然后将.flatMap应用于zip序列的元组元素:
let array1 = ["Albert","Bobby"]
let array2 = ["Charles", "David"]
let arrayMerged = zip(array1,array2).flatMap{ [$0.0, $0.1] }
print(arrayMerged) // ["Albert", "Charles", "Bobby", "David"]
#2
-3
Give this a shot
试一试
var a = ["one", "two"]
var b = ["three", "four"]
var c = a + b
print(c)
#1
12
You can use zip
to combine your two arrays, and thereafter apply a .flatMap
to the tuple elements of the zip sequence:
您可以使用zip来组合两个数组,然后将.flatMap应用于zip序列的元组元素:
let array1 = ["Albert","Bobby"]
let array2 = ["Charles", "David"]
let arrayMerged = zip(array1,array2).flatMap{ [$0.0, $0.1] }
print(arrayMerged) // ["Albert", "Charles", "Bobby", "David"]
#2
-3
Give this a shot
试一试
var a = ["one", "two"]
var b = ["three", "four"]
var c = a + b
print(c)