I have list of maps and I want to get the keys.
[%{"$fields" => ["id", "title"], "provider" => ["id", "public_name"]}]
Here I want to get the provider
and $fields
keys.
How can I do this?
thanks
I have list of maps and I want to get the keys.
[%{"$fields" => ["id", "title"], "provider" => ["id", "public_name"]}]
Here I want to get the provider
and $fields
keys.
How can I do this?
thanks
Roughly like this:
list
|> Enum.map(&Map.keys/1)
|> Enum.concat
|> Enum.uniq
This is untested though, as I’m on my mobile only. Also this is a very naïve approach, there might be more efficient solutions available.
If we’re golfing:
list
|> Enum.flat_map(&Map.keys/1)
|> Enum.uniq
In my backmind there is a note saying flat_map were deprecated… Is this note lying?
Or should this note be about other easily composable functions in Enum?
Edit
I looked at enums sources… I was confused with filter_map (it’s with f as well )