neuone

neuone

List with nested maps

How would I map over something like this?

results = [
  {"John Smith", %{"twitter" => "jsmith"}},
  {"Lisa Smith", %{"twitter" => "lsmith", "website" => "lsmith.com"}}
]

Into something like this

new_results = [
  %{name: "John Smith", twitter: "jsmith", website: ""},
  %{name: "Lisa Smith", twitter: "lsmith", website: "lsmith.com"}
]

Each entry might not have a twitter_handle or website, but I still need to have those entries. Even if the values of them are blank.

I’m think I need to Pattern match to get the values out of the results?

# Pattern Match A
{username, %{"twitter"=> twitter_handle}} = {"John Smith", %{"twitter" => "jsmith"}}

username = "John Smith"
twitter_handle = "jsmith"

# Pattern Match B
{username, %{"twitter"=> twitter_handle, "website"=> website}} = {"Lisa Smith", %{"twitter" => "lsmith", "website" => "lsmith.com"}}

username = "Lisa Smith"
twitter_handle = "lsmith"
website = "lsmith.com"

And then Enum.map into a new list?

Looking for some feedback on how I’m approaching this problem.

Marked As Solved

kokolegorille

kokolegorille

iex> results |> Enum.map(fn {username, map} = _tuple -> %{name: username, twitter: Map.get(map, "twitter"), website: Map.get(map, "website") } end) 
[%{name: "John Smith", twitter: "jsmith", website: nil},
 %{name: "Lisa Smith", twitter: "lsmith", website: "lsmith.com"}]

if You want to avoid nil value, you can use map get with default value.

Also Liked

kokolegorille

kokolegorille

def atomize_keys(map) do
map
|> Enum.map(fn {k, v} → {String.to_atom(k), v} end)
|> Enum.into(%{})
end

When I see a map into, I am tempted to prefer reduce like this

iex> map |> Enum.reduce(%{}, fn ({k, v}, acc) -> Map.put(acc, String.to_atom(k), v) end)

Where Next?

Popular in Questions Top

Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement