Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir - update a map which has string keys

Tags:

elixir

How do I update a map which has string keys? I want to update the "brand" value.

My code (product is a map with "brand" key):

  brand = URI.decode(product["brand"])
  IO.inspect(brand, label: "uri decode")
  brand = elem(Poison.decode(brand), 1)
  IO.inspect(brand, label: "json decode")
  Map.put(product, "brand", brand)
  IO.inspect(product["brand"], label: "actual product brand")

outputs:

uri decode: "\"e&ggsssssaaqss\""
json decode: "e&ggsssssaaqss"
actual product brand: "%22e%26ggsssssaaqss%22"

It isn't updating product["brand"]

The actual product brand log should equal the json decode log if it gets updated.

What am I doing wrong?

like image 208
BeniaminoBaggins Avatar asked Nov 20 '25 15:11

BeniaminoBaggins


1 Answers

If a map has string keys like so:

my_map = %{"a" => 1, "b" => 2}

You can create a new map with the changed key like this:

my_new_map = Map.put(my_map, "a", 100)

Or you can rebind the existing my_map variable with the updated map like so:

my_map = Map.put(my_map, "a", 100)
like image 87
timbuckley Avatar answered Nov 22 '25 07:11

timbuckley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!