- 对于原始集合里的每一个元素,以一个变化后的元素替换之后形成一个新的集合
let numbers = [1, 2, 4, 5, 10]
print(numbers.map({ $0 * 10 }))
- 对于原始集合离得每一个元素,通过判定来将其丢弃或者放进新集合
let numbers = [1, 2, 4, 5, 10]
print(numbers.filter({ $0 > 4 }))
- 对于原始集合里的每一个元素,作用域当前累积的结果上
let numbers = [1, 2, 4, 5, 10]
print(numbers.reduce(100, { $0 + $1}))
- 对于元素是集合的集合,可以得到单级的集合
let results = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
let allResults = results.flatMap({ $0.map({ $0 * 10 }) })
let passMarks = results.flatMap({ $0.filter({ $0 > 5 }) })
print(allResults)
print(passMarks)
- 过滤空值
let keys: [String?] = ["zhangsan", nil, "", nil, "wangwu"]
let validNames = keys.compactMap({ $0 })
print(validNames)
let counts = keys.compactMap({ $0?.count })
print(counts)
网友评论