I am facing a challenge with merging two arrays. Most solutions I have come across suggest using the concat
method. However, my requirement is to add a key/value pair from array1
to each object in array2
.
The first array (array1) that needs to be merged:
[
"Basket Abandonment",
"Downloads",
"App Version"
]
This is the second array (array2):
[
{
bottom: {
comp : "",
details, : "3.1.39 22nd Jul 2015",
status : "",
title : "Previous Version",
value : "8.7%"
},
top: {
details: "3.1.40 25th August 2015",
status: "",
comp: "",
title: "Latest Version",
value: "86%",
}
},
{
bottom: {
value: "469",
title: "Total Reviews",
status: "neutral",
comp: "same",
details: "2 New This Week"
},
top: {
details: "Version 3.1.40",
status: "neutral",
comp: "same",
title: "Average Rating",
value: "4.0"
}
},
{
bottom: {
value: "469",
title: "Total Reviews",
status: "neutral",
comp: "same",
details: "2 New This Week"
},
top: {
details: "Version 3.1.40",
status: "neutral",
comp: "same",
title: "Average Rating",
value: "4.0"
}
}
]
In the final combined array, we want to introduce a new key named title
for each object by assigning values from the first array. The resulting array should appear as follows:
[
{
title: "Basket Abandonment",
bottom: {
comp : "",
details, : "3.1.39 22nd Jul 2015",
status : "",
title : "Previous Version",
value : "8.7%"
},
top: {
details: "3.1.40 25th August 2015",
status: "",
comp: "",
title: "Latest Version",
value: "86%",
}
},
{
title: "Downloads",
bottom: {
value: "469",
title: "Total Reviews",
status: "neutral",
comp: "same",
details: "2 New This Week"
},
top: {
details: "Version 3.1.40",
status: "neutral",
comp: "same",
title: "Average Rating",
value: "4.0"
}
},
{
title: "App Version",
bottom: {
value: "469",
title: "Total Reviews",
status: "neutral",
comp: "same",
details: "2 New This Week"
},
top: {
details: "Version 3.1.40",
status: "neutral",
comp: "same",
title: "Average Rating",
value: "4.0"
}
}
]