redpoll

redpoll

Dealing with an array in phoenix

Hello again with my “questions” :slight_smile:
Having this in my live phx1.6

def handle_event("do_something", %{"array" => attrs}, socket) do
    (...)
end

where attrs would be:

%{user1_id: 1, user2_id: 2}

I can do this for example:

attrs["user1_id"]

which will obviously return 1. But how do I update this user1_id, let’s say I want to change the value of user1_id to 5.

attrs["user1_id"] = 5

won’t do it. Tried this: Map.update!(%{user1_id: 1}, :user1_id, &(&5)), also array |> Map.replace!(:user1_id, "8") Stupit question again, but can’t figure it out :slight_smile:

Best Regards

Most Liked

Bumppoman

Bumppoman

While this is very true, I want to point out the likelihood that OP has the opposite issue: attrs likely uses string keys, coming from a Phoenix form, and can’t be updated or accessed using atoms.

shd42

shd42

First, attrs is not an array, it’s a map. And you can use:

attrs = Map.replace(attrs, :user1_id, 5).

Also, about Map.update!/3, that’s not how the capture operator (&) works. The anonymous function in that function only provides a single parameter (the current value). If you use &(&5), you’re trying to access the fifth parameter being provided, which does not exist in this case. Doing it like this Map.update!(attrs, :user1_id, &(&1 + 1)) would means that i’m taking the current value, and adding one to it.

andrewb

andrewb

One way to update a value in a map is to replace it using Map.put/3. It will replace or add any key-value pair. There is also Map.put_new if you don’t want to create a new key-value- pair.

attrs = %{ "user_id" => 1 }

 Map.put(attrs, "user_id", 5)
# %{ "user_id" => 5 }

Where Next?

Popular in Discussions Top

Qqwy
I would like to spark a discussion about the static access operator: .. For whom does not know: it is used in Elixir to access fields of...
New
mmport80
I have put far too much effort into Dialyzer over the last year or so - and basically - I doubt it’s worth the effort. It’s not as easy ...
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18146 126
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New

Other popular topics Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New

We're in Beta

About us Mission Statement