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 #3

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

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement