Filter array of dictionaries - SWIFT -
i have following array:
[[user1id: nsdate], [user2id: nsdate], [user3id: nsdate]]
can filtered out dictionary user2id
?
let test = [["user1id": nsdate()], ["user2id": nsdate()], ["user3id": nsdate()]] let newarray = test.filter { $0.keys.contains("user2id") }.flatmap { $0 } print(newarray) // ["user2id": 2016-01-28 10:52:29 +0000]
i've edited dictionary bit testing purposes.
answer comment:
if want know if array contains dictionary "user2id" key can following:
test.contains { $0.keys.contains("user2id") } ? print("yep!") : print("nope!") // "yep!"
Comments
Post a Comment