I've got an array of hashes and I need to figure out which hash has the smallest height attributes. So given this array,
[{"height"=>130, "source"=>"http.facebook.com/tiny_pic.jpg", "width"=>173},
{"height"=>1230, "source"=>"http.facebook.com/giant_pic.jpg", "width"=>1273}]
I want to return this
{"height"=>130, "source"=>"http.facebook.com/tiny_pic.jpg", "width"=>173}
What's the most efficient way to do this?
确保它只是一个数组,所以:
array_of_hashes.min_by {|h| h['height']}
使用min
方法:
hash = array.min { |a,b| a['height'] <=> b['height'] }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.