我有这个哈希:
{:id => [RN1223,BN3233], :name => ['Red Ring','Blue Bangles'], :category => ['Ring','Bangles']}
我想用以下格式获得一个新的Hash:
{:id => 'RN1223', :name => 'Red Ring', :category => 'Ring'},
{:id => 'BN3233', :name => 'Blue Bangles', :category => 'Bangles'}
答案 0 :(得分:1)
hsh = {:id => ['RN1223','BN3233'],
:name => ['Red Ring','Blue Bangles'],
:category => ['Ring','Bangles']}
hsh.values.transpose.map {|arr| Hash[[:id, :name, :category].zip arr]}
# => [{:id=>"RN1223", :name=>"Red Ring", :category=>"Ring"},
# {:id=>"BN3233", :name=>"Blue Bangles", :category=>"Bangles"}]