我有一系列哈希(或至少我认为它们是哈希),我需要为每个哈希取出ID。我确信ruby有一些快速的方法可以做到这一点......我无法弄明白。
我不想遍历数组并构建一个新数组。
[
[
{
"bio": "I am a tech geek who loves starting up companies. While I was in college, I founded Squeeze My Tees",
"business_name": "Rounded Development",
"city": "",
"created_at": "2012-04-22T18:07:44Z",
"first_name": "Brian",
"id": 1,
"industry": "Entertainment",
"last_name": "Weinreich",
"lat": null
},
{
"access_token": null,
"bio": null,
"business_name": null,
"city": null,
"created_at": "2012-04-23T13:56:35Z",
"email": "test@jambo.com",
"first_name": "asdad",
"id": 2,
"industry": null,
"last_name": "ddfs",
"lat": null,
"linkedin_id": null,
"linkedin_url": null,
"lng": null,
"position": null,
"professional_headline": null,
"state": null,
"street": null,
"updated_at": "2012-04-23T13:56:35Z"
},
{
"access_token": null,
"bio": null,
"business_name": null,
"city": null,
"created_at": "2012-04-23T13:56:39Z",
"email": "tesasdat@jambo.com",
"first_name": "fdsd",
"id": 3,
"industry": null,
"last_name": "asdgw",
"lat": null,
"linkedin_id": null,
"linkedin_url": null,
"lng": null,
"position": null,
"professional_headline": null,
"state": null,
"street": null,
"updated_at": "2012-04-23T13:56:39Z"
},
{
"access_token": null,
"bio": null,
"business_name": null,
"city": null,
"created_at": "2012-04-23T13:56:44Z",
"email": "asdsad@jambo.com",
"first_name": "ewtrwef",
"id": 4,
"industry": null,
"last_name": "dfd",
"lat": null,
"linkedin_id": null,
"linkedin_url": null,
"lng": null,
"position": null,
"professional_headline": null,
"state": null,
"street": null,
"updated_at": "2012-04-23T13:56:44Z"
}
]
]
答案 0 :(得分:1)
只需提取ID即可:
the_IDs = array_of_hashes.collect { |single_array| single_array["id"] }
显然,您可以使用较少的详细变量名称,它们仅用于说明。但是这个想法是你可以遍历数组并收集块返回的内容。在这种情况下,您将继续获取ID,而the_IDs
将只是收集的数据的数组。