I am trying to count particular field value in a list of maps. Below is the map which I have.
I got this result from my ecto query.
query = from t in Tracks,
where: t.admin_id == ^admin_id and t.destination == ^location_id,
join: d in Driver, on: t.driver_id == d.id,
join: c in Company, on: d.company_id == c.id,
select: %{source: t.source, destination: t.destination, box_ids: t.box_ids, status: t.status, driver: d.name, company: c.name}
Repo.all(query)
[
%{
company: "FEDEX",
destination: "c6f072a7-5334-436f-a498-fa815871327a",
driver: "David",
source: "b27af8e4-656c-414a-9033-275ca8a888ef",
status: 0
},
%{
company: "FEDEX",
destination: "c6f072a7-5334-436f-a498-fa815871327a",
driver: "David",
source: "b27af8e4-656c-414a-9033-275ca8a888ef",
status: 1
},
%{
company: "FEDEX",
destination: "c6f072a7-5334-436f-a498-fa815871327a",
driver: "David",
source: "b27af8e4-656c-414a-9033-275ca8a888ef",
status: 1
}
]
In the above map i want to have to count of how many status 0 and how many has status 1.
Status 0 = pending
Status 1 = delivered
So I will have the result of
pending = 1
delivered = 2
I am trying with Enum.reduce but I could not able to achieve the result
pending = Enum.reduce(track_level, 0, fn x, acc ->
if x.status == 0 do
acc + 1
end
end)
Can anyone give me insight on how to achieve this? Your help is greatly appreciated.