Basically I want to filter this kind of list:
[
%{name: "gigi", priority: 2, value: 2},
%{name: "gigi", priority: 1, value: 1},
%{name: nil, priority: 1, value: 1},
%{name: nil, priority: 1, value: 2}
]
and get this list as a result:
[
%{name: "gigi", priority: 2, value: 2},
%{name: nil, priority: 1, value: 1},
%{name: nil, priority: 1, value: 2}
]
The are already sorted desc by name and priority.
I tried this code, it works but does not look nice at all to me:
arr
|> Enum.group_by(&Map.get(&1, :name))
|> Enum.map(fn
{nil, v} -> v
{_, v} -> List.first(v)
end)
|> List.flatten()
Is there a more optimum way to filter the array?