I have two lists with maps:
apps = [%{name: "app1", version: "0.0.1"}, %{name: "app2", version: "0.0.1"}]
updates_list = [%{name: "app1", version: "0.0.2"},
%{name: "app2", version: "0.0.1"},
%{name: "app3", version: "0.0.1"},
%{name: "app4", version: "0.0.1"}]
and need to enumerate through the updates_list
, to put new key into each element in the apps
list to show if there are updates. i.e. Here there is new version for app1
, I need to add new_version: true/false
and get the desired result:
result = [%{name: "app1", version: "0.0.1", new_version: true},
%{name: "app2", version: "0.0.1", new_version: false}]
I have tried with the following function:
updates_map = Enum.map(updates_list, fn (x) -> Map.put(%{}, x.name, x.version ) end)
result = Enum.map(apps, fn (x) -> map_updates(x, updates_map) end)
and function:
defp map_updates(app, updates_map) do
app_map = Map.from_struct(app)
for update <- updates_map do
if Map.has_key?(update, app_map.name) do
if Map.fetch(update, app_map.name) != app_map.version do
app_map = app_map |> Map.put(:new_version, true)
else
app_map = app_map |> Map.put(:new_version, false)
end
else
app_map = app_map
end
end
app_map
end
But because it enumerates through all updates_map
for every app, it just overrides it, and I get no result. How can exit the loop, or preserve the changes to achieve the result
as above? Any advise appreciated!