Add values in the map in list

Hello,
I have a list like
[
%Component{
allocated_quantity: 1,
component_handle: “q_cdo_01”,
component_id: 8804323,
kind: “quantity_based”,
name: “CALL”,
scheme: “per_unit”,
subscription_id: 313729351,
unit_name: “user”
},
%Component{
allocated_quantity: 1,
component_handle: “q_cdu_01”,
component_id: 8804234,
kind: “quantity_based”,
name: “DUMMY”,
scheme: “per_unit”,
subscription_id: 313729351,
unit_name: “user”
},
%Component{
allocated_quantity: 4,
component_handle: “a_api_01”,
component_id: 9230020,
kind: “quantity_based\t”,
name: “API”,
scheme: “per_unit”,
subscription_id: 313729351,
unit_name: “user”
}
]
I want to compare the last allocated_quantity value(which is 4 in this case) with sum of other allocated_quantity(1 and 1 in this case).

Is there any way to perform the above scenario?

Maybe like this.

iex> list = [%{a_q: 1}, %{a_q: 1}, %{a_q: 4}]
[%{a_q: 1}, %{a_q: 1}, %{a_q: 4}]
iex> [h|t] = Enum.reverse(list)
iex> h.a_q > Enum.reduce(t, 0, fn x, acc -> acc + x.a_q end)
true
3 Likes

Thank you soo much… It worked… :grinning: