Convert array of hashes to array

后端 未结 3 1948
一向
一向 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 01:44
    [{:code=>"404"}, {:code=>"302"}, {:code=>"200"}].flat_map(&:values)
    #⇒ ["404", "302", "200"]
    
    0 讨论(0)
  • 2021-01-22 01:48
    a=[{:code=>"404"}, {:code=>"302"}, {:code=>"200"}] 
    puts a.map{|x|x.values}.flatten.inspect
    

    output

    ["404", "302", "200"]
    
    0 讨论(0)
  • 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"]
    
    0 讨论(0)
提交回复
热议问题