Elixir getting information from a list of maps -
i have structure being sent me via post: contacts = [%{"john" => ["0724573977"]}, %{"mary" => ["0724573111", "0744556778"]}]
list of contacts each contact being map name => phone_numbers
i'm trying make list phone numbers, ignoring names, list of numbers:
a = [%{"1" => ["0724573977"]}, %{"2" => ["0724573111", "0744556778"]}] p = enum.reduce a, [], fn(contact, acc) -> {_record_id, phones} <- contact phone <- phones acc ++ phone end end end io.inspect p
this 1 produces: [[[["0724573977"] | "0724573111"], [["0724573977"] | "0744556778"]]]
odd don't know doing wrong.
this may not idiomatic elixir i'm new language works me:
iex(11)> list.flatten(enum.map(a, fn(x) -> map.values(x) end)) ["0724573977", "0724573111", "0744556778"]
Comments
Post a Comment