Convert array of hashes to array

后端 未结 3 1947
一向
一向 2021-01-22 01:28

I have an array of hashes from which I need the values of the hashes in a new array. The array of hashes look likes this, with a couple thousand of them.

array =         


        
3条回答
  •  伪装坚强ぢ
    2021-01-22 02:06

    arr =[{:code=>"404"}, {:code=>"302"}, {:code=>"200"}]
    
    arr.map { |h| h[:code] }
      #=> ["404", "302", "200"]
    

    or, if the name of the key (now :code) might change in future:

    arr.map { |h| h.first.last }
      #=> ["404", "302", "200"]
    

提交回复
热议问题