Swift阵列平面图

时间:2019-03-14 21:20:31

标签: swift

这是我使用Realm数据库的问题的简化表示。

struct Foo: CustomStringConvertible {
    var fooId: Int
    var fooName: String
    var bars: [String]

    var description: String {
        return "Foo: \(fooId) \(fooName) \(bars)"
    }
}

var ff = [Foo]()

var f1 = Foo(fooId: 1, fooName: "A", bars: ["A1", "A2", "A3"])
var f2 = Foo(fooId: 2, fooName: "B", bars: ["B1"])
var f3 = Foo(fooId: 3, fooName: "A", bars: ["A4", "A5"])
var f4 = Foo(fooId: 4, fooName: "B", bars: ["B2", "B3", "B4"])
var f5 = Foo(fooId: 5, fooName: "B", bars: ["B5"])
var f6 = Foo(fooId: 6, fooName: "A", bars: ["A6", "A7", "A8", "A9", "A10"])
var f7 = Foo(fooId: 7, fooName: "C", bars: ["C1", "C2", "C3"])

ff.append(contentsOf: [f1, f2, f3, f4, f5, f6, f7])

我想对基于fooName的Foo数组进行flatMap或compactMap,因此我能够构建fooName-> bar的Map

  

A-> [“ A1”,“ A2”,“ A3”,“ A4”,“ A5”,“ A6”,“ A7”,“ A8”,“ A9”,“ A10”]   B-> [“ B1”,“ B2”,“ B3”,“ B4”,“ B5”]   C-> [“ C1”,“ C2”,“ C3”]

2 个答案:

答案 0 :(得分:4)

您可以reduce(into:_:)

let results = ff.reduce(into: [String: [String]]()) { result, foo in
    result[foo.fooName, default: []].append(contentsOf: foo.bars)
}

答案 1 :(得分:2)

这似乎很简单:

// group instances by fooName
let dict: Dictionary<String, [Foo]> = Dictionary(grouping: ff, by: { $0.fooName })
print(dict)
// flat map the [Foo] instances to [String] of their bars
let dict2: Dictionary<String, [String]> = dict.mapValues { foos in foos.flatMap { $0.bars } }
print(dict2)

或简化:

let dict = Dictionary(grouping: ff, by: { $0.fooName })
    .mapValues { foos in foos.flatMap { $0.bars } }
print(dict)