Reputation: 3888
How to get a specific dictionary from array of dictionary by giving key alone in method.
we studied about indexOf method in array but we are not sure about how to pass key and get a index of particular key.
Here is Array of dictionary
[
{
"Twitter0":"NSLayoutConstraint"
},
{
"Twitter1":"NSLayoutConstraint"
},
{
"Insta2":"NSLayoutConstraint"
},
{
"Twitter3":"NSLayoutConstraint"
},
{
"Insta4":"NSLayoutConstraint"
}
]
I just want to pass key name like Twitter2 in method.
We tried this Link, we dont know how to process with key
Thanks in advance..
Upvotes: 2
Views: 114
Reputation: 270860
You can use firstIndex(where:)
and pass a closure to it:
arrayOfDicts.firstIndex(where: { $0.keys.contains("Twitter2")})
Or more generally with key
:
arrayOfDicts.firstIndex(where: { $0.keys.contains(key)})
Upvotes: 2