Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Filtering maps

map_filtering.livemd

Filtering maps

Example Data

fields = [%{field: "accessionnumber"}]
studies = [
      %{patientname: "Carmack^John", patientid: 1, accessionnumber: "A001"},
      %{patientname: "Kernighan^Brian", patientid: 2, accessionnumber: "A002"},
      %{patientname: "Torvalds^Linus", patientid: 3, accessionnumber: "A003"},
      %{patientname: "Van Rossum^Guido", patientid: 4, accessionnumber: "A004"},
      %{patientname: "Valim^José", patientid: 5, accessionnumber: "A005"}
    ]
[
  %{patientname: "Carmack^John", patientid: 1, accessionnumber: "A001"},
  %{patientname: "Kernighan^Brian", patientid: 2, accessionnumber: "A002"},
  %{patientname: "Torvalds^Linus", patientid: 3, accessionnumber: "A003"},
  %{patientname: "Van Rossum^Guido", patientid: 4, accessionnumber: "A004"},
  %{patientname: "Valim^José", patientid: 5, accessionnumber: "A005"}
]

Instead of elimating the fields we don’t want, just use fields to get the fields we do want. Since the map keys are atoms, we need to convert the fields to atoms as well. We also assume the shape of the fields is a list of maps where each map has one key, field.

field_names =
  fields
  |> Enum.map(&Map.get(&1, :field))
  |> Enum.map(&String.to_atom(&1))
[:accessionnumber]

Now we enumerate the list of patients and extract the required fields.

Enum.map(studies, &Map.take(&1, field_names))
[
  %{accessionnumber: "A001"},
  %{accessionnumber: "A002"},
  %{accessionnumber: "A003"},
  %{accessionnumber: "A004"},
  %{accessionnumber: "A005"}
]