How to convert map keys from strings to atoms in Elixir

前端 未结 13 1182
梦毁少年i
梦毁少年i 2021-01-31 07:07

What is the way to convert %{\"foo\" => \"bar\"} to %{foo: \"bar\"} in Elixir?

13条回答
  •  北海茫月
    2021-01-31 07:30

    I think the easiest way to do this is to use Map.new:

    %{"a" => 1, "b" => 2} 
    |> Map.new(fn {k, v} -> {String.to_atom(k), v} end)      
    
    => %{a: 1, b: 2}
    

提交回复
热议问题