我有一个dic数组,在我的dic中,我有一个键叫做“ state”:test,“ state”:original。我可能有多个对象。
码:
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
中有4个dic对象。在那个如果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,]
最佳答案
你可以试试这个
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
如果有任何问题,您可以询问。