I have two arrays.
我有两个数组。
let A = ["91","91","49"]
let B = ["9989898909","9089890890","9098979896"]
I need to merge these arrays and show it in the dropdown as
我需要合并这些数组并在下拉列表中显示它
["91 9989898909","91 9089890890","49 9098979896"]
[“91 9989898909”,“91 9089890890”,“49 9098979896”]
How can I get this result using swift.Im newbie to swift,can anyone please help on this.
我如何使用swift得到这个结果。我是swift的新手,有谁可以帮忙解决这个问题。
3 个解决方案
#1
7
Zip the arrays and concatenate the results:
压缩数组并连接结果:
let A=["91","91","49"]
let B=["9989898909","9089890890","9098979896"]
let zipped = zip(A, B)
let result = zipped.map { $0.0 + " " + $0.1 }
#2
1
let A = ["91","91","49", "5"]
let B = ["9989898909","9089890890","9098979896"]
Use zip()
to join values from both arrays A
and B
. If A
and B
have a different number of elements, the joining would still work. then map
the tuples from the zipped result array to those elements with a space between them
使用zip()连接数组A和B的值。如果A和B具有不同数量的元素,则连接仍然有效。然后将压缩结果数组中的元组映射到它们之间有空格的元素
let C : [String] = zip(A,B).lazy.map() {$0 + " " + $1}
The lazy
is just to improve the performance for large sequences.
懒惰只是为了改善大型序列的性能。
In your question the third element in C
is "91 9098979896"
and not "49 9098979896"
在你的问题中,C中的第三个元素是“91 9098979896”而不是“49 9098979896”
#3
1
here is a snippet in Swift:
这是Swift中的一个片段:
let a = ["90", "91", "92"]
let b = ["80012", "82379", "123712"]
let result: [String] = a.enumerated().map { (index, element) in
return index < b.count ? element + " " + b[index] : element
}
#1
7
Zip the arrays and concatenate the results:
压缩数组并连接结果:
let A=["91","91","49"]
let B=["9989898909","9089890890","9098979896"]
let zipped = zip(A, B)
let result = zipped.map { $0.0 + " " + $0.1 }
#2
1
let A = ["91","91","49", "5"]
let B = ["9989898909","9089890890","9098979896"]
Use zip()
to join values from both arrays A
and B
. If A
and B
have a different number of elements, the joining would still work. then map
the tuples from the zipped result array to those elements with a space between them
使用zip()连接数组A和B的值。如果A和B具有不同数量的元素,则连接仍然有效。然后将压缩结果数组中的元组映射到它们之间有空格的元素
let C : [String] = zip(A,B).lazy.map() {$0 + " " + $1}
The lazy
is just to improve the performance for large sequences.
懒惰只是为了改善大型序列的性能。
In your question the third element in C
is "91 9098979896"
and not "49 9098979896"
在你的问题中,C中的第三个元素是“91 9098979896”而不是“49 9098979896”
#3
1
here is a snippet in Swift:
这是Swift中的一个片段:
let a = ["90", "91", "92"]
let b = ["80012", "82379", "123712"]
let result: [String] = a.enumerated().map { (index, element) in
return index < b.count ? element + " " + b[index] : element
}