如何将所有Enum.map
值转换为字符串?
result =
Enum.map(
from(Scheme, where: ^whitelisted_params) |> limit(^query_limit) |> Repo.all(),
fn elem ->
elem |> Map.from_struct() |> Map.delete(:__meta__)
end
)
在该计划中,有string
,bool
和int
类型
使用to_string / 1函数
这是一个数组,其中整数,浮点数,字符串和布尔值转换为字符串
iex> [1, 1.0, "str", true] |> Enum.map(& to_string/1)
["1", "1.0", "str", "true"]
我想你正在寻找Kernel.inspect/2
。与IO.inspect/2
不同,它返回字符串而不是将其打印为副作用。
iex> Enum.map(["I'm a string", true, 42], &inspect/1)
["\"I'm a string\"", "true", "42"]
虽然可能由S.B.的评论暗示,但你的问题听起来像是XY question。我想你应该在你的问题中写下你为什么试图转换成字符串。如果你解释你想要完成的更大的任务,有人可能能够提供更好的解决方案。