flatMap
let array: [[String]] = [["apple", "banana"], ["grape", "peach"]]
let flatArray = array.flatMap { (innerArray) -> [String] in
return innerArray
}
Swift.print(flatArray)
// -> ["apple", "banana", "grape", "peach"]
compactMap
let array: [String?] = ["apple", "banana", nil, "grape"]
let nonNilArray = array.compactMap { (str) -> String? in
return str
}
Swift.print(nonNilArray)
// -> ["apple", "banana", "grape"]
私は$0
を使う省略記法あまり好きではないです...