Backo
Backo

Reputation: 18881

How to retrieve values related to a hash key present in a Array of Hashes

I am using Ruby on Rails 3.2.2 and I would like to retrieve values related to a hash key present in a Array of Hashes. That is, I have the following Array of Hashes:

[
  {
    :key1 => value_a_1,
    :key2 => value_a_2
  },
  {
    :key1 => value_b_1,
    :key2 => value_b_2
  },
  {
    :key1 => value_c_1,
    :key2 => value_c_2
  }
]

I would like to "retrieve" / "build" the following:

[ value_a_1, value_b_1, value_c_1 ]

How can I make that the proper way?

Upvotes: 0

Views: 352

Answers (1)

rfunduk
rfunduk

Reputation: 30452

If a is the array:

a.map { |i| i[:key1] }

Upvotes: 2

Related Questions