I got two array which seems similar, part of them are duplicate, and I need to combine them.
Here is the first array.
first
[
{
:group_id => "12873",
:question_sets => [
{
:id => 29435,
:name => "Question1"
},
{
:id => 29349,
:name => "Question2"
},
]
},
{
:group_id => "12876",
:question_sets => [
{
:id => 29443,
:name => "Question3"
}
]
}
]
Here is second array
[
{
:group_id => "12873",
:question_sets => [
{
:id => 29435,
:name => "Question1"
},
{
:id => 29338,
:name => "Question4"
},
]
},
{
:group_id => "12888", #(not in first array)
:question_sets => [
{
:id => 29443,
:name => "Question3"
}
]
}
]
The idea was to combine the question_sets's id and name by the same group_id, every record in second array need to be combined. If there is no the same group_id, create the group_id.
The result will be like
[
{
:group_id => "12873",
:question_sets => [
{
:id => 29435,
:name => "Question1"
},
{
:id => 29349,
:name => "Question2"
},
{
:id => 29338,
:name => "Question4"
}
]
},
{
:group_id => "12876",
:question_sets => [
{
:id => 29443,
:name => "Question3"
}
]
},
{
:group_id => "12888",
:question_sets => [
{
:id => 29443,
:name => "Question3"
}
]
}
]
arr1 = [ { :group_id => "12873",...). That way, readers can refer to those variables in answers and comments without having to define them (and they will all use the same variable names, which is convenient for running code). One thing that was not clear about your question prompted me to make an assumption that is given in the first line of my answer. You might want to clarify that with an edit.