Home > Blockchain >  How to combine 2 array
How to combine 2 array

Time:03-25

I have 2 arrays of objects, I want to combine both array of objects as one array, by adding 2nd arrays key value pair as 3rd key value pairs for 1st array.

First array

 a = [{:performed_at=>"Aging", :result=>"Not Completed"}, {:performed_at=>"Aging", :result=>"Not Completed"}, {:performed_at=>"Mar 25, 2022", :result=>"Pass"}]

Second Array

b = [{:line_item=>"10C-1648150055"}, {:line_item=>"10D-1648150055"}, {:line_item=>"10E-1648150055"}]

I want the result array of objects like this

[{:performed_at=>"Aging", :result=>"Not Completed", :line_item=>"10C-1648150055"}, {:performed_at=>"Aging", :result=>"Not Completed",:line_item=>"10D-1648150055"}, {:performed_at=>"Mar 25, 2022", :result=>"Pass",:line_item=>"10E-1648150055"}]

Kindly help

CodePudding user response:

Something like the this should work:

a.map.with_index do |el, index|
 el.merge(b[index])
end
  • Related