I am trying to pattern match a key value in a map and discard everything else.
identity = RedditOAuth2.get_identity(access_token)
# here is how i am getting the key "name" right now.
name = Map.get(identity, "name")
#----------------------------------------
# How would i do something like this
%{"name" => name | rest} = RedditOAuth2.get_identity(access_token)
You have have multiple =
in a single match.
%{"name" => name} = identity = RedditOAuth2.get_identity(access_token)
identity
will have the entire map assigned to it and name
will have whatever was in the "name"
key.
If you're wanting to discard everything else from the identity and are okay adding another function, you may be looking for Map.split/2
.
{%{"name" => name}, identity} =
access_token
|> RedditOAuth2.get_identity()
|> Map.split(["name"])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With