I'm grappling with a challenge involving an array containing two objects. After using Promise All to fetch both of these objects, I've hit a roadblock in trying to merge them dynamically. Despite experimenting with various array methods like map, forEach, and others, I have yet to find one that fits my requirements.
My attempts with the map function have fallen short numerous times. Each time, it results in being assigned to a new array, not serving my purpose of needing it as an object. My goal is to merge a fluctuating number of objects within the array seamlessly, but so far, this task has proven tricky.
This snippet showcases my use of promise all:
let response = await Promise.all(
key.map(async tag => {
let params = { params: { tag: tag } };
let promise = await axios.get(url, params);
return promise.data;
})
);
Below lies my unsuccessful endeavor at solving the problem:
let merged = response.map(x => {
let obj = { ...x };
return obj;
});
The JSON data presented here has been condensed for clarity purposes.
[
{
"posts": [
{
"author": "Zackery Turner",
"authorId": 12,
"id": 2,
"tags": [
"startups",
"tech",
"history"
]
}
]
},
{
"posts": [
{
"author": "Rylee Paul",
"authorId": 9,
"id": 1,
"tags": [
"tech",
"health"
]
}
]
}
]
I'm feeling stuck and unsure about finding a dynamic solution to this problem.
Essentially, I'm aiming for an output in this format:
{
"posts": [
{
"author": "Zackery Turner",
"authorId": 12,
"id": 2,
"tags": [
"startups",
"tech",
"history"
]
},
{
"author": "Rylee Paul",
"authorId": 9,
"id": 1,
"tags": [
"tech",
"health"
]
}
I acknowledge the complexity of seeking a dynamic solution rather than resorting to hard coding, which appears to be the common advice found on similar discussions on platforms like StackOverflow.