Is there an easy way in ruby to split an array of hashes into mutiple arrays by common key? -
if have array this:
[{:x=>1, :y=>a, :z=>i}, {:x=>2, :z=>ii}, {:x=>3, :y=>b, :z=>iii}, {:x=>4, :z=>iv}, {:x=>5, :y=>c, :z=>v}] is there easy 1 liner arrays each common key this?:
[[1,2,3,4,5], [a, b, c], [i, ii, iii, iv, v]]
if have variable pointing array:
arr = [{:x=>1, :y=>a, :z=>i}, {:x=>2, :z=>ii}, {:x=>3, :y=>b, :z=>iii}, {:x=>4, :z=>iv}, {:x=>5, :y=>c, :z=>v}] then can write this:
%i{x y z}.map { |key| arr.map { |subarr| subarr[key] }.compact }
Comments
Post a Comment