[2012, 10]}, {"key"=>[2012,...">All geek questions in one placeGroup an array of hashes by a given key and accumulate valuesSource (already ordered correctly):rows = [{"key"=>[2012, 10]}, {"key"=>[2012, 9]}, {"key"=>[2011, 7]}] Desired Result:[[2012, [10, 9]], [2011, [7]]] -1rubySammy Oct 22 '12 at 11:23source share2 answersrows.map {|row| row.values.flatten}.inject({}) {|h,r| h[r[0]].nil? ? h[r[0]] = [r[1]] : h[r[0]] << r[1];h }.to_a # [[2012, [10, 9]], [2011, [7]]] orrows.map {|row| row.values.flatten}.inject({}) {|h,r| h[r[0]] ||= []; h[r[0]] << r[1];h }.to_a # [[2012, [10, 9]], [2011, [7]]] +1saihgala Oct 22 '12 at 11:53source shareSince the data is pre-ordered, we can use chunkinstead group_by:rows.chunk { |h| h.values.first[0] }.map do |year, hs| [year, hs.map { |h| h.values.first[1] }] end #=> [[2012, [10, 9]], [2011, [7]]] +2tokland Oct 22 '12 at 12:11source shareSource: https://habr.com/ru/post/1661599/More articles:Processing view logic in reduction - javascriptHow to use a generic parameter type for another generic class? - genericsIn Angular2, while I have a named socket open, how can I go to another route And close the named socket? - angularA group array of hashes by key, followed by values - ruby | fooobar.comЗадача не может быть загружена из сборки - c#Firebase how to dynamically search for a keystroke under user data - javascriptAdding line breaks when using interpolation - javascriptHow to use wait in lambda python - pythonNuGet configuration file for VS2015 solution (NuGet 3.4+) - visual-studio-2015Проверьте, присутствует ли какой-либо конкретный класс в HTML с помощью beautifulsoup Python - pythonAll Articles
Source (already ordered correctly):
rows = [{"key"=>[2012, 10]}, {"key"=>[2012, 9]}, {"key"=>[2011, 7]}]
Desired Result:
[[2012, [10, 9]], [2011, [7]]]
rows.map {|row| row.values.flatten}.inject({}) {|h,r| h[r[0]].nil? ? h[r[0]] = [r[1]] : h[r[0]] << r[1];h }.to_a # [[2012, [10, 9]], [2011, [7]]]
or
rows.map {|row| row.values.flatten}.inject({}) {|h,r| h[r[0]] ||= []; h[r[0]] << r[1];h }.to_a # [[2012, [10, 9]], [2011, [7]]]
Since the data is pre-ordered, we can use chunkinstead group_by:
chunk
group_by
rows.chunk { |h| h.values.first[0] }.map do |year, hs| [year, hs.map { |h| h.values.first[1] }] end #=> [[2012, [10, 9]], [2011, [7]]]
Source: https://habr.com/ru/post/1661599/More articles:Processing view logic in reduction - javascriptHow to use a generic parameter type for another generic class? - genericsIn Angular2, while I have a named socket open, how can I go to another route And close the named socket? - angularA group array of hashes by key, followed by values - ruby | fooobar.comЗадача не может быть загружена из сборки - c#Firebase how to dynamically search for a keystroke under user data - javascriptAdding line breaks when using interpolation - javascriptHow to use wait in lambda python - pythonNuGet configuration file for VS2015 solution (NuGet 3.4+) - visual-studio-2015Проверьте, присутствует ли какой-либо конкретный класс в HTML с помощью beautifulsoup Python - pythonAll Articles