I have two arrays of hashes and I would like to merge them.
One array has the array of:
{:id, :name}
The other, an array of:
{:name, :other_attributes*}
I’ve been raking my brains for hours on this one. Is there anyone who
can suggest the best way to merge these two arrays so that I can get the
resulting array of:
{:id. :name, :other_attributes}
And I would like the resulting array to look like:
array #1: [{:other_attributes => “stuff”, :name=>“File1.rar”,
:id=>“a3d80425a51a3a4cb2d7a3201439ef6cc3af5874”}, {:name=>“File2.avi”,
:id=>“58a658047ccb7333eb812da02dca9d84c1db2b41”, :other_attributes =>
“stuff”}, {:name=>“File3.avi”,
:id=>“944f6a7c50a4a5cff0c28cb0f9158b16580b0310”, :other_attributes =>
“stuff”}]
But I just have no idea how to do it. I’m sure there’s an easy ruby way
to do it somehow…
And I would like the resulting array to look like:
array #1: [{:other_attributes => “stuff”, :name=>“File1.rar”,
:id=>“a3d80425a51a3a4cb2d7a3201439ef6cc3af5874”}, {:name=>“File2.avi”,
:id=>“58a658047ccb7333eb812da02dca9d84c1db2b41”, :other_attributes =>
“stuff”}, {:name=>“File3.avi”,
:id=>“944f6a7c50a4a5cff0c28cb0f9158b16580b0310”, :other_attributes =>
“stuff”}]
But I just have no idea how to do it. I’m sure there’s an easy ruby way
to do it somehow…
Will work, and can be readily improved (exercise for reader - it’ll
be
niceer without turning the data in to an array of rows as I do in first
two lines).
I haven’t measured the time, but this might have better performance
than finding in the second array by
name every time, since here we are accessing a hash by that key.
Hope this helps,
Jesus.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.