Array to Hash Ruby

后端 未结 9 474
无人及你
无人及你 2021-01-29 17:43

Okay so here\'s the deal, I\'ve been googling for ages to find a solution to this and while there are many out there, they don\'t seem to do the job I\'m looking for.

Ba

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-29 18:22

    Or if you have an array of [key, value] arrays, you can do:

    [[1, 2], [3, 4]].inject({}) do |r, s|
      r.merge!({s[0] => s[1]})
    end # => { 1 => 2, 3 => 4 }
    

提交回复
热议问题