Swift Array 常见操作
var arr = [1, 3, 5, 7];
// [2, 6, 10, 14]
let arr2 = arr.map( {$0 * 2 } )
// 组装返回值类型数组
// ["ABC_2", "ABC_6", "ABC_10", "ABC_14"]
let arr3 = arr.map( { "ABC_\($0 * 2)" } )
// [[2, 2], [6, 6], [10, 10], [14, 14]]
let arr4 = arr.map( { Array.init(repeating: $0, count: 2) } )
// 返回数组值
// [2, 2, 6, 6, 10, 10, 14, 14]
let arr5 = arr.flatMap( { Array.init(repeating: $0, count: 2) } )
let arrStr = ["1", "zhangsan", "lisi", "4"];
// 返回数组值
// [1, nil, nil, 4]
let arr6 = arrStr.map( { Int($0) } )
// 去除结构后的nil
// [1, 4]
let arr7 = arrStr.compactMap( { Int($0) } )
arr = [1, 3];
// 此次遍历用到上次的值
// [2, 8]
let arr8 = arr.reduce(0, { $0 + $1 * 2 } )
// 数组懒加载
let arr9 = arrStr.lazy.map( { Int($0) } )
思路来源《小码哥教育》