代码之家  ›  专栏  ›  技术社区  ›  Bitwise

更新深嵌套映射中的单个值

  •  0
  • Bitwise  · 技术社区  · 8 年前

    我有这张地图:

    %{
      __meta__: #Ecto.Schema.Metadata<:loaded, "questions">,
      __struct__: MyApp.Question,
      active: true,
      description: "player points",
      id: 118,
      inserted_at: ~N[2018-08-26 19:48:22.501445],
      reserved: %Statcasters.Questions.Reserved{
        information: %{
          game_id: "b796cbe9-0bb6-4aaf-98b0-5da81c337208",
          player_id: "8ffb69ce-9a6b-44a6-8e8f-c069235d2d31",
          player_name: "Lebron James"
        },
        inputs: [%{label: "Player Points", type: "text"}]
      },
      type: "NBA",
      updated_at: ~N[2018-08-26 19:48:22.504193]
    }
    

    reserved.information.player_id 8ffb69ce-9a6b-44a6-8e8f-c069235d2d31 12345 ?

    在Ruby中,由于易变性,这是一个很小的变化,但是我很难找到用长生不老药更新它的最佳方法。

    Map.put(map, :player_id, "053600fb-3aae-422f-a9cb-9d102cca301f")
    

    这不起作用,因为它只是将玩家id添加到地图的第一级。

    3 回复  |  直到 8 年前
        1
  •  1
  •   Aleksei Matiushkin    8 年前

    使用 Kernel.put_in/3 :

    input = %{
      reserved: %{
        information: %{player_id: 42}
      }
    }
    
    put_in(input, ~w|reserved information player_id|a, -1)
    #⇒ %{reserved: %{information: %{player_id: -1}}}
    

    如果您想更改/调整值而不是简单地放置新值,请使用 Kernel.get_and_update_in/3 . 让后者合作 ,此结构应实现 Access

        2
  •  1
  •   7stud    8 年前

    如何更新保留。信息。玩家id从 8ffb69ce-9a6b-44a6-8e8f-c069235d2d31至12345?

    defmodule Statcasters.Questions.Reserved do
      defstruct information: %{}, inputs: []
    end
    
    defmodule My do
    
      def go do
        map = %{
          __meta__: "hello world",
          __struct__: "boo hoo",
          active: true,
          description: "player points",
          id: 118,
          inserted_at: ~N[2018-08-26 19:48:22.501445],
          reserved: %Statcasters.Questions.Reserved{
            information: %{
              game_id: "b796cbe9-0bb6-4aaf-98b0-5da81c337208",
              player_id: "8ffb69ce-9a6b-44a6-8e8f-c069235d2d31",
              player_name: "Lebron James"
            },
            inputs: [%{label: "Player Points", type: "text"}]
          },
          type: "NBA",
          updated_at: ~N[2018-08-26 19:48:22.504193]
        }
    
        put_in(map.reserved.information.player_id, 12345)
      end
    
    
    end
    

    在iex中:

    ~/elixir_programs$ iex
    Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
    
    Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)
    
    iex(1)> c "my.exs"
    [My, Statcasters.Questions.Reserved]
    
    iex(2)> My.go     
    %{
      __meta__: "hello world",
      __struct__: "boo hoo",
      active: true,
      description: "player points",
      id: 118,
      inserted_at: ~N[2018-08-26 19:48:22.501445],
      reserved: %Statcasters.Questions.Reserved{
        information: %{
          game_id: "b796cbe9-0bb6-4aaf-98b0-5da81c337208",
          player_id: 12345,
          player_name: "Lebron James"
        },
        inputs: [%{label: "Player Points", type: "text"}]
      },
      type: "NBA",
      updated_at: ~N[2018-08-26 19:48:22.504193]
    } 
    
    iex(3)> 
    
        3
  •  0
  •   Adam Millerchip    7 年前

    struct :

    put_in(struct.reserved.information.player_id, "12345")