如何在Elixir中将映射键从字符串转换为原子 [英] How to convert map keys from strings to atoms in Elixir
本文介绍了如何在Elixir中将映射键从字符串转换为原子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
转换%{ foo =>的方式是什么? bar}
到Elixir中的%{foo: bar}
?
What is the way to convert %{"foo" => "bar"}
to %{foo: "bar"}
in Elixir?
推荐答案
使用理解:
iex(1)> string_key_map = %{"foo" => "bar", "hello" => "world"}
%{"foo" => "bar", "hello" => "world"}
iex(2)> for {key, val} <- string_key_map, into: %{}, do: {String.to_atom(key), val}
%{foo: "bar", hello: "world"}
这篇关于如何在Elixir中将映射键从字符串转换为原子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文