This is my array
[{:amount=>10, :gl_acct_id=>1, :alt_amount=>20}, {:amount=>20, :gl_acct_id=>2 , :alt_amount=>30}]
i want result
[{:amount => 30}] or {:amount = 30}
Any idea?
Ruby versions >= 2.4.0 has an Enumerable#sum method. So you can do
arr.sum {|h| h[:amount] }
array.map { |h| h[:amount] }.sum
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With