如何连接Elixir中映射属性的列表



我有。。。

%{
"errors" => %{
"abc" => ["example", "something"],
"xyz" => ["thing goes here"]
}
}

我想要。。。

["example", "something", "thing goes here"]

去那里最干净的方法是什么?

如果您不需要错误键优先的顺序保证,您可以将Map.values/1List.flatten/1结合起来,或者使用理解来非常容易地完成

map = %{
"errors" => %{
"abc" => ["example", "something"],
"xyz" => ["thing goes here"]
}
}
map["errors"]
|> Map.values()
|> List.flatten()
#=> ["example", "something", "thing goes here"]
for {_key, items} <- map["errors"], item <- items, do: item
#=> ["example", "something", "thing goes here"]

最新更新