拥有它:
[%{"id" => "1", "name" => "foo"},
%{"id" => "2","name" => "bar"}]
如何将其转换为以下内容:
[%"foo" => %{"id" => "1","name" => "foo"},
%"bar" => {"id" => "2","name" => "bar"}]
答案 0 :(得分:2)
假设您需要Map
String
个密钥,可以for
使用into:
,如下所示:
iex(1)> entries = [%{"id" => "1", "name" => "foo"}, %{"id" => "2","name" => "bar"}]
[%{"id" => "1", "name" => "foo"}, %{"id" => "2", "name" => "bar"}]
iex(2)> for %{"name" => name} = entry <- entries, into: %{}, do: {name, entry}
%{"bar" => %{"id" => "2", "name" => "bar"},
"foo" => %{"id" => "1", "name" => "foo"}}