Rails mapping array of hashes onto single hash

后端 未结 4 1086
情书的邮戳
情书的邮戳 2021-01-30 12:08

I have an array of hashes like so:

 [{\"testPARAM1\"=>\"testVAL1\"}, {\"testPARAM2\"=>\"testVAL2\"}]

And I\'m trying to map this onto sin

4条回答
  •  天涯浪人
    2021-01-30 13:05

    Use #inject

    hashes = [{"testPARAM1"=>"testVAL1"}, {"testPARAM2"=>"testVAL2"}]
    merged = hashes.inject({}) { |aggregate, hash| aggregate.merge hash }
    merged # => {"testPARAM1"=>"testVAL1", "testPARAM2"=>"testVAL2"}
    

提交回复
热议问题