<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="213245" data-post-id="213245">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="lkuty" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lkuty/120/34879_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  lkuty
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>There is a really interesting Hex package named <a href="https://hexdocs.pm/lens/" rel="noopener nofollow ugc"><code>lens</code></a> (or on <a href="https://github.com/obrok/lens" rel="noopener nofollow ugc">github</a>) which allows to easily perform manipulations of deeply nested maps.</p>
<p>Below you’ll find two examples. The first updates <code>user_id</code> without any context (key of current map value) and the second has the context to allow updating <code>user_id</code> with the value of the key (0 or 1). <code>Lens.into/2</code> is used to reconstruct a map out of the list we get from <code>Lens.all/0</code>.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(1)&gt; m = %{
...(1)&gt;   "zip"         =&gt; "",
...(1)&gt;   "location"    =&gt; "",
...(1)&gt;   "user_sports" =&gt; %{
...(1)&gt;     "0" =&gt; %{"sport_id" =&gt; "1", "user_id" =&gt; ""},
...(1)&gt;     "1" =&gt; %{"sport_id" =&gt; "1", "user_id" =&gt; ""},
...(1)&gt;   },
...(1)&gt; }
%{
  "location" =&gt; "",
  "user_sports" =&gt; %{
    "0" =&gt; %{"sport_id" =&gt; "1", "user_id" =&gt; ""},
    "1" =&gt; %{"sport_id" =&gt; "1", "user_id" =&gt; ""}
  },
  "zip" =&gt; ""
}
iex(2)&gt; lens1 = Lens.key("user_sports") |&gt; Lens.map_values() |&gt; Lens.key("user_id")
#Function&lt;41.112152499/3 in Lens.seq/2&gt;
iex(3)&gt; Lens.map(lens1, m, fn _v -&gt; "updated" end)
%{
  "location" =&gt; "",
  "user_sports" =&gt; %{
    "0" =&gt; %{"sport_id" =&gt; "1", "user_id" =&gt; "updated"},
    "1" =&gt; %{"sport_id" =&gt; "1", "user_id" =&gt; "updated"}
  },
  "zip" =&gt; ""
}
iex(4)&gt; lens2 = Lens.key("user_sports") |&gt; Lens.into(Lens.context(Lens.all(),  Lens.at(1) |&gt; Lens.key("user_id")), %{})
#Function&lt;41.112152499/3 in Lens.seq/2&gt;
iex(5)&gt; Lens.map(lens2, m, fn {{id, _map}, _v} -&gt; "updated with #{id}" end)
%{
  "location" =&gt; "",
  "user_sports" =&gt; %{
    "0" =&gt; %{"sport_id" =&gt; "1", "user_id" =&gt; "updated with 0"},
    "1" =&gt; %{"sport_id" =&gt; "1", "user_id" =&gt; "updated with 1"}
  },
  "zip" =&gt; ""
}
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="213245" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/manipulate-nested-map/13464/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-213245" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="213245"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>