我正在尝试将结构转换为映射,以便能够清除所有 nil 值
我目前正在使用此代码
case Nadia.get_updates do
{:ok, results} ->
Map.from_struct(results)
|> Enum.filter(fn {_, v} -> v != nil end)
|> Enum.into(%{})
注意:Nadia.get_updates 返回以下结构: https://hexdocs.pm/nadia/Nadia.Model.Update.html#t:t/0
但是我总是收到以下错误:Map.from_struct/1 中没有匹配的函数子句
最佳答案
自 v0.15我们有 Map.from_struct/1
正是这样做的。
defmodule User do
defstruct [:name]
end
Map.from_struct(%User{name: "valim"})
#=> %{name: "valim"}
关于dictionary - Elixir 将结构转换为映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36512627/