I have a JSON array:
response = [
%{
"created_at" => 1542757526,
"email" => "[email protected]",
"first_name" => "rana",
"id" => "YW1pcnBheWFyeUB5YWhvby5jb20=",
"last_clicked" => nil,
"last_emailed" => nil,
"last_name" => "amir",
"last_opened" => nil,
"updated_at" => 1542759123
},
%{
"created_at" => 1542757457,
"email" => "[email protected]",
"first_name" => "rana",
"id" => "cmFtaXIyNDI2QGdtYWlsLmNvbQ==",
"last_clicked" => nil,
"last_emailed" => nil,
"last_name" => "amir",
"last_opened" => nil,
"updated_at" => 1542759001
},
# .......
]
I'm trying to get the email
field of all items in the response
variable. Example:
["[email protected]", "[email protected]", ....]
You're looking for
Enum.map/2
. This method calls the passed function on every item in the given list/enumerable:Alternatively, you can use the shorthand and make it concise:
External Resources: See this and also this to understand the concept of mapping in functional programming in general.
Side note:
flat_map/2
is a variation ofmap/2
that expects the "mapped result" to be another list (so it can be joined and flattened with the rest of the mapped results).