Reputation: 827
I have got an array of hashes, for example:
[{"id" => "1", "name" => "Name 1"},
{"id" => "2", "name" => "Name 2"},
{"id" => "3", "name" => "Name 3"}]
I would like to get the value of the key "name"
for each hash, similar to this:
["Name 1", "Name 2", "Name 3"]
I looked around for quite a while but couldn't find the answer I was looking for.
Upvotes: 0
Views: 880
Reputation: 51151
It's simplest to use Enumerable#map
for this purpose:
array = [{"id" => "1", "name" => "Name 1"}, {"id" => "2", "name" => "Name 2"}, {"id" => "3", "name" => "Name 3"}]
array.map { |hash| hash['name'] }
# => ["Name 1", "Name 2", "Name 3"]
Upvotes: 1