合并具有更多列表和项目的列表项

Merge list items having more list and items

我正在处理恰好有此类项目的列表

[
  %{localtions: [], doctors: [%{name: "Junaid", hospital: "west"}, %{name: "Farooq", hospital: "test"}]},
  %{localtions: [%{name: "Dom", address: "test"}, %{name: "Domi", address: "west"}], doctors: []},
  %{localtions: [], doctors: []},
  %{localtions: [%{name: "Dominic", address: "test"}, %{name: "DomDom", address: "west"}], doctors: []}
]

我只是想把它转换成这个。

[locations: [%{name: "Domi", address: "test"}, %{name: "Dom", address: "test"}%{name: "Dominic", address: "test"},  %{name: "DomDom", address: "west"}], doctors: [%{name: "Junaid", hospital: "west"}, %{name: "Farooq", hospital: "test"}]]

任何帮助都会很棒,谢谢。

您可以根据需要使用这些功能:

t = [
    %{
      localtions: [],
      doctors: [%{name: "Junaid", hospital: "west"}, %{name: "Farooq", hospital: "test"}]
    },
    %{
      localtions: [%{name: "Dom", address: "test"}, %{name: "Dom", address: "west"}],
      doctors: []
    },
    %{localtions: [], doctors: []},
    %{
      localtions: [%{name: "Dominic", address: "test"}, %{name: "DomDom", address: "west"}],
      doctors: []
    }
  ]

函数定义为:

  def name_is_unique(l) do
    Enum.reduce(l, %{}, fn x, mp ->
      Map.merge(mp, x, fn _k, v1, v2 ->
        Enum.reduce(v1 ++ v2, %{}, fn x, acc -> Map.put(acc, x.name, x) end)
        |> Map.values()
      end)
    end)
  end

  def duplication_check(l) do # with duplication check
    Enum.reduce(l, %{}, fn x, mp ->
      Map.merge(mp, x, fn _k1, mpV1, mpV2 ->
        (mpV1 ++ mpV2) # [%{name: "Dom"}, %{name: "Dom"}]
        |> Enum.reduce(MapSet.new(), fn inerListMap, inerMapSet ->
          MapSet.put(inerMapSet, inerListMap) # %{name: "Dom"}
        end)
        |> MapSet.to_list()
      end)
    end)
  end

  def with_duplication(l) do
    Enum.reduce(l, %{}, fn x, mp ->
      Map.merge(mp, x, fn _k1, mpV1, mpV2 ->
        mpV1 ++ mpV2
      end)
    end)
  end

如果您的唯一性是 name 键,则使用 name_is_unique 或者如果您希望地图项具有完全唯一性,请使用 duplication_check 并且不关心重复使用 with_duplication

name_is_unique(t)
%{
  doctors: [
    %{hospital: "test", name: "Farooq"},
    %{hospital: "west", name: "Junaid"}
  ],
  localtions: [
    %{address: "west", name: "Dom"},
    %{address: "west", name: "DomDom"},
    %{address: "test", name: "Dominic"}
  ]
}

duplication_check(t)
%{
  doctors: [
    %{hospital: "test", name: "Farooq"},
    %{hospital: "west", name: "Junaid"}
  ],
  localtions: [
    %{address: "test", name: "Dom"},
    %{address: "test", name: "Dominic"},
    %{address: "west", name: "Dom"},
    %{address: "west", name: "DomDom"}
  ]
}

with_duplication(t)
%{
  doctors: [
    %{hospital: "west", name: "Junaid"},
    %{hospital: "test", name: "Farooq"}
  ],
  localtions: [
    %{address: "test", name: "Dom"},
    %{address: "west", name: "Dom"},
    %{address: "test", name: "Dominic"},
    %{address: "west", name: "DomDom"}
  ]
}

输入:

input_list = [
  %{
    doctors: [
      %{hospital: "west", name: "Junaid"},
      %{hospital: "test", name: "Farooq"}
    ],
    localtions: []
  },
  %{
    doctors: [],
    localtions: [
      %{address: "test", name: "Dom"},
      %{address: "west", name: "Domi"}
    ]
  },
  %{doctors: [], localtions: []},
  %{
    doctors: [],
    localtions: [
      %{address: "test", name: "Dominic"},
      %{address: "west", name: "DomDom"}
    ]
  }
]

模块:

defmodule Items do
  def sanitize(input_list),
    do: Enum.reduce(input_list, [localtions: [], doctors: []], &update_items(&2, &1))

  defp update_items([localtions: localtions_list, doctors: doctors_list], %{
         localtions: new_localtions,
         doctors: new_doctors
       }),
       do: [localtions: localtions_list ++ new_localtions, doctors: doctors_list ++ new_doctors]
end

IEX 会话:

iex()> Items.sanitize(input_list)

结果:

[
  localtions: [
    %{address: "test", name: "Dom"},
    %{address: "west", name: "Domi"},
    %{address: "test", name: "Dominic"},
    %{address: "west", name: "DomDom"}
  ],
  doctors: [
    %{hospital: "west", name: "Junaid"},
    %{hospital: "test", name: "Farooq"}
  ]
]