How to condense summable metrics to a unique identifier in a ruby table

后端 未结 3 644
挽巷
挽巷 2021-01-21 13:43

I\'m trying to condense summable metrics to a unique identifier in a ruby table.

I have the following table:

[[\"id1\", 123], [\"id2\", 234], [\"id1\", 3         


        
3条回答
  •  北海茫月
    2021-01-21 14:17

    summable_metrics = [["id1", 123],["id2", 234], ["id1", 345]]
    
    summable_metrics.group_by(&:first).map do |k, v|
      [k, v.map(&:last).reduce(:+)]
    end
    
    => [["id1", 468], ["id2", 234]] 
    

提交回复
热议问题