0

我有一组 dic,在我的 dic 中我有一个键,称为“状态”:测试,“状态”:原始。我可能有多个对象。

代码:

var StatusData = [[String: Any]]()
var testStatusData = [[String: Any]]()
var originalStatusData = [[String: Any]]()

我的方法中的小片段:

哪里-> document.data() is an [String:Any]

for document in (querySnapshot! as AnyObject).documents {
                    print("\(document.documentID) => \(document.data())")
                    let data  = document.data()
                    self.playersStatusData.append(document.data())
                }
                print(self.playersStatusData)

现在我需要添加过滤器playersStatusData,并基于状态测试,原始。我需要分离对象并附加到它们各自的 dic 数组中。

如果我的playersStatusData. 如果有 2 个对象具有状态 =“测试”。然后,仅该特定对象就需要附加到testStatusData.

同样的另一个 2 对象具有状态 =“原始”。然后,仅该特定对象就需要附加到originalStatusData.

我用过filter { $0.1 == "orginal" }。它只是打印那个键值。但我需要完整的对象并附加到它们各自的 [String: Any]]。

任何帮助,谢谢。

更新 :

 [“test”: 1, “id”: 230, “total”: 1, “crunch”: 1, “name”: Cristina Criado, "Bot": <null>, "state": test,]

 [“test”: 3, “id”: 20, “total”: 10, “crunch”: 1, “name”: phasni, "Bot": <null>, "state": original,]

[“test”: 5, “id”: 0, “total”: 00, “crunch”: 1, “name”:picturn, "Bot": <null>, "state": test,]
4

1 回答 1

0

你可以试试这个

let playersStatusData =  [
    ["test": 1, "id": 230, "total": 1, "crunch": 1, "name": "A", "Bot": "", "state": "test"],
    ["test": 2, "id": 231, "total": 1, "crunch": 1, "name": "B", "Bot": "", "state": "original"],
    ["test": 3, "id": 232, "total": 1, "crunch": 1, "name": "C", "Bot": "", "state": "test"],
    ["test": 4, "id": 233, "total": 1, "crunch": 1, "name": "D", "Bot": "", "state": "original"],
    ["test": 5, "id": 234, "total": 1, "crunch": 1, "name": "E", "Bot": "", "state": "test"],
    ["test": 6, "id": 235, "total": 1, "crunch": 1, "name": "F", "Bot": "", "state": "test"]
]


let dicts = Dictionary(grouping: playersStatusData, by: {$0["state"] as! String})

print(dicts["test"]) // O/P of all state = test in array
print(dicts["original"]) // O/P of all state = original in array

如果有什么问题可以问。

于 2019-07-11T10:45:41.180 回答