Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下格式的数据结构:
data_hash = [ { price: 1, count: 3 }, { price: 2, count: 3 }, { price: 3, count: 3 } ]
有没有一种有效的方法来获取:price像数组一样的值[1,2,3]?
:price
[1,2,3]
首先,如果您使用的是 ruby < 1.9:
array = [ {:price => 1, :count => 3}, {:price => 2, :count => 3}, {:price => 3, :count => 3} ]
然后得到你需要的东西:
array.map{|x| x[:price]}