1
有它:更新仙丹地圖
[%{"id" => "1", "name" => "foo"},
%{"id" => "2","name" => "bar"}]
如何把它轉換爲是這樣的:
[%"foo" => %{"id" => "1","name" => "foo"},
%"bar" => {"id" => "2","name" => "bar"}]
有它:更新仙丹地圖
[%{"id" => "1", "name" => "foo"},
%{"id" => "2","name" => "bar"}]
如何把它轉換爲是這樣的:
[%"foo" => %{"id" => "1","name" => "foo"},
%"bar" => {"id" => "2","name" => "bar"}]
假設你想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"}}
第二個片段是無效的語法。你的意思是'%{「foo」=> ...,「bar」=> ...}? – Dogbert
你有什麼嘗試?我們沒有人可以幫助你,除非我有一些工作,特別是[最小,完整和可驗證示例(MCVE)](https://stackoverflow.com/help/mcve)。解釋這個問題的一個好方法是包含你迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出以及實際獲得的輸出(控制檯輸出,回溯等)。您提供的細節越多,您可能會收到的答案就越多。另外,請查看[常見問題](http://meta.stackexchange.com/q/7931)和[問](https://stackoverflow.com/help/asking)。 –