我有以下清单
nl =
[
{"_id", "idhere"},
{"App-Version", "version_here"},
{"TX-ID", "tx-id-here"},
{"Method", "method-here"},
{"Body",
{[
{"Identification",
[
{"A", "A-Val"},
{"B", "B-Val"},
{"C", "C-Val"},
{"D", "D-Val"}
]},
{"CurrentTime", "2023-02-23T16:40:13+05:30"},
{"Names", ["Name1", "Name2"]}
]}}
]
如何在 Elixir 中将其转换为嵌套地图?
我尝试使用 Map.new,但我是 Elixir 的新手,无法通过。
从 elixirforum 得到解决方案(由 andreaseriksson 回答)
def transform([{_, _}|_] = list) do
Enum.reduce(list, %{}, fn {key, val}, map ->
Map.put(map, key, transform(val))
end)
end
def transform({[{_, _}|_] = list}), do: transform(list)
def transform(val), do: val