此问题已在此处有答案:
Separate odd and even indexes in a sequence array(4个答案)
13天前关闭
我有一个数组arrays1,我如何将这个数组拆分为下面的2个数组?
let arrays1 = [1, 3, 2, 4, 5, 7, 6, 8, 9, 10]
我的期望:
let listItems1 = [1, 2, 5, 6, 9]
let listItems2 = [3, 4, 7, 8, 10]
下面是我所有的代码,我想尝试将数字10从listItems1移动到listItems2
let arrays1 = [1,3,2,4,5,7,6,8,9,10]
let arrays2 = arrays1.chunked(into: 2)
let listItems1 = arrays2.enumerated().filter { $0.offset % 2 == 0 }.flatMap { $0.element }
let listItems2 = arrays2.enumerated().filter { $0.offset % 2 != 0 }.flatMap { $0.element }
extension Array {
func chunked(into size: Int) -> [[Element]] {
return stride(from: 0, to: count, by: size).map {
Array(self[$0 ..< Swift.min($0 + size, count)])
}
}
}
1条答案
按热度按时间eanckbw91#