Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert hash to array of hashes

I have hash, all its values are arrays, like this:

list = { letter:  ['a', 'b', 'c'],
         number:  ['one', 'two', 'three'],
         fruit:   ['apple', 'pear', 'kiwi'],
         car:     ['vw', 'mb', 'bmw'],
         state:   ['la', 'ny', 'fl'],
         color:   ['red', 'white', 'black'],
         tree:    ['oak', 'pine', 'maple'],
         animal:  ['cat', 'dog', 'rat'],
         clothes: ['tie', 'sock', 'glove'] }

In fact this hash could have more keys and values could be larger, but always sizes of every value are the same (in this case - three).

I want to convert this hash to array of hashes.

Each hash will have all keys of origin hash and respective value.

So finally I want to have:

list = [
  { letter: 'a', number: 'one', fruit: 'apple', car: 'vw', state: 'la',
    color: 'red', tree: 'oak', animal: 'cat', clothes: 'tie' },

  { letter: 'b', number: 'two', fruit: 'pear', car: 'mb', state: 'ny',
    color: 'white', tree: 'pine', animal: 'dog', clothes: 'sock' },

  { letter: 'c', number: 'three', fruit: 'kiwi', car: 'bmw', state: 'fl',
    color: 'black', tree: 'elm', animal: 'rat', clothes: 'glove' }
]

What's the best way to do it?

like image 662
mechnicov Avatar asked Dec 01 '25 05:12

mechnicov


1 Answers

[list.keys].product(list.values.transpose).map { |a| a.transpose.to_h }
   #=> [{:letter=>"a", :number=>"one", :fruit=>"apple", :car=>"vw", 
   #     :state=>"la", :color=>"red", :tree=>"oak", :animal=>"cat",
   #     :clothes=>"tie"},
   #    {:letter=>"b", :number=>"two", :fruit=>"pear", :car=>"mb",
   #     :state=>"ny", :color=>"white", :tree=>"pine", :animal=>"dog",
   #     :clothes=>"sock"},
   #    {:letter=>"c", :number=>"three", :fruit=>"kiwi", :car=>"bmw",
   #     :state=>"fl", :color=>"black", :tree=>"maple", :animal=>"rat", 
   #     :clothes=>"glove"}]

Suppose list were defined as follows.

list = {
  letter:  ['a',     'b'   ],
  number:  ['one',   'two' ],
  fruit:   ['apple', 'pear'],
  car:     ['vw',    'mb'  ]
}

The steps would be as follows.

b = [list.keys]
  #=> [[:letter, :number, :fruit, :car]]
c = list.values
  #=> [["a", "b"], ["one", "two"], ["apple", "pear"], ["vw", "mb"]]
d = c.transpose
  #=> [["a", "one", "apple", "vw"],
  #    ["b", "two", "pear",  "mb"]]
e = b.product(d)
  #=> [[[:letter, :number, :fruit, :car], ["a", "one", "apple", "vw"]],
  #    [[:letter, :number, :fruit, :car], ["b", "two", "pear",  "mb"]]]
e.map { |a| a.transpose.to_h }
  #=> [{:letter=>"a", :number=>"one", :fruit=>"apple", :car=>"vw"},
  #    {:letter=>"b", :number=>"two", :fruit=>"pear",  :car=>"mb"}]

Let's look at the last step more closely. map passes the first element of e to the block and sets the block variable a to its value:

a = e.first
  #=> [[:letter, :number,  :fruit,  :car],
  #    ["a",     "one",    "apple", "vw"]]

The block calculation is as follows.

f = a.transpose
  #=> [[:letter, "a"], [:number, "one"], [:fruit, "apple"], [:car,   "vw"]]
f.to_h
  #=> {:letter=>"a", :number=>"one", :fruit=>"apple", :car=>"vw"}

The remaining calculations for e.map { |a| a.transpose.to_h } are similar.

like image 191
Cary Swoveland Avatar answered Dec 03 '25 18:12

Cary Swoveland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!