Bitwise
Bitwise

Reputation: 8461

Match variable with a map value

I have a variable set to player_id, it's a random uuid. But it matches 1 of 17 different maps. I want to be able to match the variable against the other maps and return the one that matches. How can I achieve this in Elixir?

Current Attempt and data:

Here is 3 of the 17 maps:

    %{"abbr_name" => "R.Baker", "birth_place" => "Scott City, KS, USA",
   "birthdate" => "1993-03-30", "college" => "Wichita State",
   "experience" => "1", "first_name" => "Ron", "full_name" => "Ron Baker",
   "height" => 76, "id" => "e8c0b33f-43b3-4490-865f-ed919d015bb2",
   "jersey_number" => "31", "last_name" => "Baker", "position" => "G",
   "primary_position" => "SG", "reference" => "1627758", "status" => "ACT",
   "updated" => "2017-11-28T22:37:19+00:00", "weight" => 220},
 %{"abbr_name" => "L.Thomas", "birth_place" => "Brooklyn, NY, USA",
   "birthdate" => "1988-04-24", "college" => "Duke",
   "draft" => %{"year" => 2010}, "experience" => "6", "first_name" => "Lance",
   "full_name" => "Lance Thomas", "height" => 80,
   "id" => "f76acd63-bf42-4fa7-9985-a7838f9cb029", "jersey_number" => "42",
   "last_name" => "Thomas", "position" => "F", "primary_position" => "SF",
   "reference" => "202498", "status" => "ACT",
   "updated" => "2017-10-17T19:37:09+00:00", "weight" => 235},
 %{"abbr_name" => "L.Kornet", "birth_place" => "Lexington, KY, USA",
   "birthdate" => "1995-07-15", "college" => "Vanderbilt",
   "draft" => %{"year" => 2017}, "experience" => "0", "first_name" => "Luke",
   "full_name" => "Luke Kornet", "height" => 85,
   "id" => "fed6929f-8d92-456c-8a89-9e0c17cf4f7b", "jersey_number" => "2",
   "last_name" => "Kornet", "position" => "C-F", "primary_position" => "C",
   "reference" => "1628436", "status" => "D-LEAGUE",
   "updated" => "2017-10-17T19:37:09+00:00", "weight" => 250}

player_id is set to e8c0b33f-43b3-4490-865f-ed919d015bb2

See how one of those ids match the player_id? How can I match the two against each and scope it so that it returns this:

%{"abbr_name" => "R.Baker", "birth_place" => "Scott City, KS, USA",
   "birthdate" => "1993-03-30", "college" => "Wichita State",
   "experience" => "1", "first_name" => "Ron", "full_name" => "Ron Baker",
   "height" => 76, "id" => "e8c0b33f-43b3-4490-865f-ed919d015bb2",
   "jersey_number" => "31", "last_name" => "Baker", "position" => "G",
   "primary_position" => "SG", "reference" => "1627758", "status" => "ACT",
   "updated" => "2017-11-28T22:37:19+00:00", "weight" => 220}

Just the full player map of what id was match to player_id?

Upvotes: 0

Views: 87

Answers (1)

Kevin Johnson
Kevin Johnson

Reputation: 1970

You have a list of maps and you would like to obtain one of those, according to some condition. In this case, you would like to retrieve that one map that has an "id" which corresponds to your player_id. In that case you can simply do:

Enum.find(your_17_maps_as_a_list, fn %{"id" => id} -> player_id == id end)

In the case you possibly may have multiple maps with the same id, and you need to retrieve all of them, then use Enum.filter

Upvotes: 2

Related Questions