Swift [map] return 包含特定值的数组 属性

Swift [map] return array containing values of specific property

假设我有一个对象数组

let persons = [Person]

struct Person { 
    let name: String
    let position: Int
}

我想要 return 字符串数组 [String] 包含位置等于 1 的人名。 如果有任何方法可以使用 map/flatmap/reduce 函数来做到这一点?

方法如下:

let names = persons
    .filter { [=10=].position == 1 }
    .map { [=10=].name }

要避免两个步骤,请使用 compactMap

let positionOneNames = persons.compactMap{[=10=].position == 1 ? [=10=].name : nil }