Advertisement
Guest User

Untitled

a guest
Jun 21st, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.41 KB | None | 0 0
  1. r = {
  2.     "list": [
  3.         {
  4.             "id": "AAA",
  5.             "total": "0.1"
  6.         },
  7.         {
  8.             "id": "AAA",
  9.             "total": "0.3"
  10.         },
  11.         {
  12.             "id": "BBB",
  13.             "total": "0.8"
  14.         }
  15.     ]
  16. }
  17.  
  18. out = r.to_h[:list].group_by { |x| x[:id] }.map do |x|
  19.     {
  20.         symbol: x[0],
  21.         total: x[1].sum { |i| i[:total].to_f }
  22.     }
  23. end
  24.  
  25. puts out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement