Ruby: efficient way to sum elements grouped by the first element of each
subarray
I have a json structure:
[["item1", 1, 0], ["item1", 1, 0], ["item1", 1, 0], ["item2", 1, 0],
["item2", 1, 0],["item3", 1, 0],["item4", 1, 0],["item5", 1, 0]..]]
right now I'm summing them manually thru loops to get the following
[["item1", 3, 0], ["item2", 2, 0], ["item4", 1, 0], ["item5", 1, 0]]
is there another way to do it? I looked at the array module in Ruby but
I'm not sure if there exist another way to do this ...
No comments:
Post a Comment