0

I have an array of hashes (or atleast I think they are hashes) and I need to pull out the ID for each of them. I'm sure ruby has some quick way of doing this ... I just can't figure it out.

I don't want to iterate through the arrays and build a new one.

[
  [
    {
      "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"
    }
  ]
]
4

1 回答 1

1

要仅提取 ID,您可以执行以下操作:

the_IDs = array_of_hashes.collect { |single_array| single_array["id"] }

显然你可以使用不那么冗长的变量名,它们只是为了说明。但这个想法是,您可以遍历一个数组并收集块返回的任何内容。在这种情况下,您会不断收到返回的 ID,并且the_IDs只是收集到的内容的数组。

于 2012-04-23T15:44:37.960 回答