如何更新另一个地图内的地图值(ELIXIR)

How to update a map value that is inside another map (ELIXIR)

我是 Elixir 的新手,我需要更改位于另一个地图中的地图的值。

例如:

test = %{"test1" => %{"test11" => 0}, "test2" => %{"test22" => 0}}

我试过了:

test = Map.put(test["test1"], "test11", 1)

Returns:

%{"test11" => 1}

但我需要:

%{"test1" => %{"test11" => 1}, "test2" => %{"test22" => 0}}

有人可以帮助我吗?

中的所有内容都是不可变的。

test =
  %{"test1" => %{"test11" => 0},
    "test2" => %{"test22" => 0}}

通过调用 Map.put(test["test1"], "test11", 1) 你不会修改任何东西,你将值放入 test["test1"] 并且 return 结果返回。

在后台使用 Kernel.put_in/3 (or update_in/3 to update) that uses Access 实现。

put_in(test, ["test1", "test11"], 1)
#⇒ %{"test1" => %{"test11" => 1},
#    "test2" => %{"test22" => 0}}