experimentix

experimentix

Update map in loop

Hello

I’m new to elixir and i’m struggling with this problem

I’ve a map lets call it order and that has several products

I want to loop through this products map inside the order map and append some info from another map called associated products

it looks like this

orders map

%{
    "_csrf_token" => "gwsgw",
    "custom_tags" => "",
    "id" => "62212",
    "products" => %{
      "123456" => %{
        "category" => "3",
        "tag" => ["hero", "superstar"],
        "title" => "Product title"
      },
      "234567" => %{"category" => "5", "title" => "Product title"},
	  
      "345678" => %{
        "category" => "8",
        "tag" => ["creation", "dynasty"],
        "title" => "Product title"
      },
      "456789" => %{
        "category" => "5",
        "title" => "Product title"
      },
    },
    "order_id" => "193885",
    "custom_message" => "Nothing to declare",
    "tags_title" => %{"tag" => ["preview"]},
    "allcats" => "5"
  }

associated products map

%{
  "123456" => [
    %{
      "id" => "4984944",
      "has_stock" => false,
      "name" => "Other product Name",
      "product_id" => "123456",
     
    },
    %{
      "id" => "3511064",
      "has_stock" => true,
      "name" => "Other product Name",
      "product_id" => "123456",
     
    }
  ],
  "234567" => [
    %{
      "id" => "3511075",
      "has_stock" => true,
      "name" => "Other product Name",
      "product_id" => "234567",
     
    },
    %{
      "id" => "3511076",
      "has_stock" => false,
      "name" => "Other product Name",
      "product_id" => "234567",
     
    }
  ],
  "345678" => [
    %{
      "id" => "3511082",
      "has_stock" => true,
      "name" => "Other product Name",
      "product_id" => "345678",
     
    },
    %{
      "id" => "3511083",
      "has_stock" => false,
      "name" => "Other product Name",
      "product_id" => "345678",
     
    }
  ]

}

i tried this code to put the associated products but it only gets updated with the last item, i assume its because it doesn’t update the order in every loop

order = for {pid, v} <- order["products"] do
        IO.inspect pid
        IO.inspect v

      order = put_in(order, ["products", pid,"associated_products"], associated_products[pid])

      end

How can i achieve this result??

%{
    "_csrf_token" => "gwsgw",
    "custom_tags" => "",
    "id" => "62212",
    "products" => %{
      "123456" => %{
        "category" => "3",
        "tag" => ["hero", "superstar"],
        "title" => "Product title",
		"associated_products" => [
			%{
			  "id" => "4984944",
			  "has_stock" => false,
			  "name" => "Other product Name",
			  "product_id" => "123456",
			 
			},
			%{
			  "id" => "3511064",
			  "has_stock" => true,
			  "name" => "Other product Name",
			  "product_id" => "123456",
			 
			}
		  ]
		
      },
      "234567" => %{"category" => "5",
		  "title" => "Product title",
		  "associated_products" => [
				%{
				  "id" => "4984944",
				  "has_stock" => false,
				  "name" => "Other product Name",
				  "product_id" => "234567",
				 
				},
				%{
				  "id" => "3511064",
				  "has_stock" => true,
				  "name" => "Other product Name",
				  "product_id" => "234567",
				 
				}
			  ]
	  },
	  
      "345678" => %{
        "category" => "8",
        "tag" => ["creation", "dynasty"],
        "title" => "Product title",
		"associated_products" => [
			%{
			  "id" => "4984944",
			  "has_stock" => false,
			  "name" => "Other product Name",
			  "product_id" => "345678",
			 
			},
			%{
			  "id" => "3511064",
			  "has_stock" => true,
			  "name" => "Other product Name",
			  "product_id" => "345678",
			 
			}
		  ]
      },
      "456789" => %{
        "category" => "5",
        "title" => "Product title",
		"associated_products" => [
			%{
			  "id" => "4984944",
			  "has_stock" => false,
			  "name" => "Other product Name",
			  "product_id" => "456789",
			 
			},
			%{
			  "id" => "3511064",
			  "has_stock" => true,
			  "name" => "Other product Name",
			  "product_id" => "456789",
			 
			}
		  ]
      },
    },
    "order_id" => "193885",
    "custom_message" => "Nothing to declare",
    "tags_title" => %{"tag" => ["preview"]},
    "allcats" => "5"
  }

Marked As Solved

stefanluptak

stefanluptak

Check this gist: update_map_in_loop.md · GitHub
It’s a Livebook, so you can copy and paste it into your Livebook server and play with it.

If you need more explanation, I can provide it. I am pretty sure there are more elegant ways to achieve this, but this should be good enough as a start.

The problem with your solution is that it’s not “reducing” the order (keeping the changes for all iterations), just returning the list of modified orders. Each order (item of the returned list) is the order modified but just for a single product.

Hope this helps a bit.

Also Liked

experimentix

experimentix

Thank you very much, it worked. And thanks for small explanation.

I think i understand how things work now looking at your code.

Once again thanks

experimentix

experimentix

Since my associated products has the key same as the product key i ended with removing some of your code and referencing it like this

Enum.reduce(order["products"], order, fn {product_id, _product}, order ->
   update_in(order, ["products", product_id], fn product ->
    Map.put(product, "associated_products", associated_products[product_id])
  end)
end)

Once again thanks

Last Post!

experimentix

experimentix

That is the behavior i want as associated product is always present.

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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
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

We're in Beta

About us Mission Statement