Reputation: 2167
I have this map stored in a customer variable
%{
billing_contact: #Ecto.Association.NotLoaded<association:billing_contact is not loaded>,
billing_contact_id: 305,
business_contact: #Ecto.Association.NotLoaded<association:business_contact is not loaded>,
business_contact_id: nil,
disabled_message: "",
end_date: nil,
id: 6851,
is_disabled: false,
name: "test",
start_date: #DateTime<2018-08-17 12:56:50.498078Z>,
technical_contact: #Ecto.Association.NotLoaded<association:technical_contact is not loaded>,
technical_contact_id: nil,
users: #Ecto.Association.NotLoaded<association :users is not loaded>
}
I want to remove all the key value pairs where value is Ecto.Association.NotLoaded
. I read Map documentation but couldn't find any function to remove key value based on value
I want to do it dynamically
. So when ever there is a map come in. It automatically remove all the key value pairs where value is Ecto.Association…
I have to send this data to the front end. so need to remove this Ecto.Association.Not loaded key value pairs.
Thanks
Upvotes: 1
Views: 889
Reputation: 23091
Here's a way using comprehensions:
for {k, v} <- customer,
%Ecto.Association.NotLoaded{} != v,
into: %{}, do: {k, v}
Upvotes: 1
Reputation: 121000
Use Enum.filter/2
:
Enum.filter(input, fn
{_, %Ecto.Association.NotLoaded{}} -> false
_ -> true
end)
Ecto.Association.NotLoaded
is a struct, hence it’s easy to pattern match it and reject all the unwanted kv-pairs.
Upvotes: 1