从哈希数组收集值 [英] Collect values from an array of hashes

查看:129
本文介绍了从哈希数组收集值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的数据结构:

I have a data structure in the following format:

data_hash = {
    [:price => 1, :count => 3],
    [:price => 2, :count => 3],
    [:price => 3, :count => 3],
}

有没有一种有效的方式来获得的值:价格像一个数组([1,2,3])

Is there an efficient way to get the values of :price as an array like( [1,2,3] )?

推荐答案

首先,正确的语法是:

array = [
    {:price => 1, :count => 3},
    {:price => 2, :count => 3},
    {:price => 3, :count => 3},
]

然后得到你所需要的:

Then to get what you need:

array.map{|x| x[:price]}

这篇关于从哈希数组收集值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆