# How to extract only needed attribute from an array

**URL:** <https://www.ruby-forum.com/t/how-to-extract-only-needed-attribute-from-an-array/173159>\
**Category:** Ruby\
**Created:** [August 4, 2009, 7:26am UTC](https://www.ruby-forum.com/t/how-to-extract-only-needed-attribute-from-an-array/173159 "2009-08-04T07:26:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [August 4, 2009, 7:26am UTC](https://www.ruby-forum.com/t/how-to-extract-only-needed-attribute-from-an-array/173159/1 "2009-08-04T07:26:53Z")

</div>

I have an object called Tags with id and name ==\> [tag.id , tag.name]

| [1 , aaa] |

| [3 , bbb] |

| [5 , ccc] |

| [6 , dfd] |

| [7 , waf] |

| [11 , vee] |

There is an array with the list of tags , i just want to extract all  
tag.id  
as a new array, what is the easiest way to do

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [August 4, 2009, 7:31am UTC](https://www.ruby-forum.com/t/how-to-extract-only-needed-attribute-from-an-array/173159/2 "2009-08-04T07:31:04Z")

</div>

> ## There is an array with the list of tags , i just want to extract all tag.id as a new array, what is the easiest way to do
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

tags.map { |t| t.id }

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [August 4, 2009, 7:33am UTC](https://www.ruby-forum.com/t/how-to-extract-only-needed-attribute-from-an-array/173159/3 "2009-08-04T07:33:59Z")

</div>

Thriving K. wrote:

> | [7 , waf] |
> 
> | [11 , vee] |
> 
> There is an array with the list of tags , i just want to extract all  
> tag.id  
> as a new array, what is the easiest way to do

Is this how your array looks?

a = [  
[1 , :aaa],  
[3 , :bbb],  
[5 , :ccc],  
[6 , :dfd],  
[7 , :waf],  
[11 , :vee]  
]

p a.transpose[0] # ==\> [1, 3, 5, 6, 7, 11]

Or:

p a.map {|b| b[0]}

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [August 4, 2009, 10:14am UTC](https://www.ruby-forum.com/t/how-to-extract-only-needed-attribute-from-an-array/173159/4 "2009-08-04T10:14:40Z")

</div>

Joel VanderWerf wrote:

> p a.transpose[0] # ==\> [1, 3, 5, 6, 7, 11]
> 
> Or:
> 
> p a.map {|b| b[0]}

Or:

p a.map {|(id,name)| id }

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [August 4, 2009, 11:49am UTC](https://www.ruby-forum.com/t/how-to-extract-only-needed-attribute-from-an-array/173159/5 "2009-08-04T11:49:14Z")

</div>

2009/8/4 Thriving K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> | [7 , waf] |
> 
> | [11 , vee] |
> 
> There is an array with the list of tags , i just want to extract all  
> tag.id  
> as a new array, what is the easiest way to do

In any case you need method #map. In 1.9\* you can even do use  
map(&:id):

irb(main):001:0\> Tag = Struct.new :id, :name  
=\> Tag  
irb(main):002:0\> a = Array.new(5) {|i| Tag.new(i, “n”\*i)}  
=\> [#, #,  
#, #,  
#]  
irb(main):003:0\> a.map(&:id)  
=\> [0, 1, 2, 3, 4]

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [August 4, 2009, 11:03am UTC](https://www.ruby-forum.com/t/how-to-extract-only-needed-attribute-from-an-array/173159/6 "2009-08-04T11:03:51Z")

</div>

Thank you for everyone
