# Array problem

**URL:** <https://www.ruby-forum.com/t/array-problem/135918>\
**Category:** Rails\
**Created:** [April 26, 2008, 10:18am UTC](https://www.ruby-forum.com/t/array-problem/135918 "2008-04-26T10:18:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rushnosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rushnosh](https://www.ruby-forum.com/u/rushnosh)\
**Post date:** [April 26, 2008, 10:18am UTC](https://www.ruby-forum.com/t/array-problem/135918/1 "2008-04-26T10:18:39Z")

</div>

a = [[ ‘john’ , ‘195’] , [‘jack’ , ‘132’] ]

i need to convert this to:

b = [[ ‘name’ , ‘john’] , [‘id’ , ‘195’] , [‘name2’ , ‘jack’] ,  
[‘id2’ , ‘132’] ]

i am throwing a blank

b = Array.new  
a.each do\_with\_index do | new, index |  
b \<\< [‘name#{index}’, new[0] ]  
b \<\< [‘id#{index}’, new[1] ]  
end

does this make sense, can’t this be done with 1 line ?

---

<div class="post-metadata">

**Author:** ![rushnosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rushnosh](https://www.ruby-forum.com/u/rushnosh)\
**Post date:** [April 26, 2008, 11:03am UTC](https://www.ruby-forum.com/t/array-problem/135918/2 "2008-04-26T11:03:22Z")

</div>

On 26 Apr 2008, at 09:17, rushnosh wrote:

> a = [[ ‘john’ , ‘195’] , [‘jack’ , ‘132’] ]
> 
> i need to convert this to:
> 
> b = [[ ‘name’ , ‘john’] , [‘id’ , ‘195’] , [‘name2’ , ‘jack’] ,  
> [‘id2’ , ‘132’] ]

a.inject([]) {| total , pair| total \<\< [‘name’, pair.first] \<\< [‘id’,  
pair.last] }

Fred
