shahryarjb

shahryarjb

How to convert map to string (separated with ,)

Hello, I have map which I want to convert it to string like this:

the map:

%{last_name: "tavakkoli", name: "shahryar"}

the string I need:

"name", "shahryar", "last_name", "tavakkoli"

it should be noted, I need to delete last “,”, and don’t need the string in list , it should be string for example

"name", "shahryar", "last_name", params."tavakkoli",

to

"name", "shahryar", "last_name", params."tavakkoli"

I tried to convert it to what I needed but I couldn’t.

Thanks.

Marked As Solved

wojtekmach

wojtekmach

Hex Core Team

Not sure if this is exactly what you wanted, but you can use Enum.map to convert each key/value into a string and then Enum.join to join strings with a comma. You can use Enum.map_join to do both at the same time:

iex> map = %{last_name: "tavakkoli", name: "shahryar"}
iex> Enum.map_join(map, ", ", fn {key, val} -> ~s{"#{key}", "#{val}"} end)
"\"last_name\", \"tavakkoli\", \"name\", \"shahryar\""

Note, maps are unordered so if the order matters for the final string you should use a different data structure for the input, e.g. keyword list.

10
Post #2

Also Liked

outlog

outlog

hmm that looks like a list?

anyways

map = %{last_name: "tavakkoli", name: "shahryar"}     
string = "name,#{map.name},last_name,#{map.last_name}"
"name,shahryar,last_name,tavakkoli"
list = ["name", map.name, "last_name", map.last_name]
["name", "shahryar", "last_name", "tavakkoli"]

though I don’t quite understand exactly what you need to output.. or does it need to be dynamic?

Map.keys(map) 
|> Enum.map(fn key -> "#{key},#{map[key]}" end)
|> Enum.join(",")
"last_name,tavakkoli,name,shahryar"

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement