Skip to content

Instantly share code, notes, and snippets.

@blackode
Last active March 10, 2020 20:48
Show Gist options
  • Save blackode/c19472093bedff864a5e6363784dd026 to your computer and use it in GitHub Desktop.
Save blackode/c19472093bedff864a5e6363784dd026 to your computer and use it in GitHub Desktop.
Nested Maps in Elixir

#Get a value from nested maps

The get_in function can be used to retrieve a nested value in nested maps using a list of keys.

nested_map = %{ name: %{ first_name: "blackode"} }     #Example of Nested Map
first_name = get_in(nested_map, [:name, :first_name])  # Retrieving the Key

# Returns nil for missing value 
nil = get_in(nested, [:name, :last_name])              # returns nil when key is not present

Read docs: Kernel.get_in/2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment