I have one array with two objects in them. I used a Promise All to get both of these. I'm having a hard time trying to merge them because I need it to be dynamic. I have been messing around with a bunch of array functions like map, forEach, etc etc but I can't seem to find any that work properly.
So I've tried the map function so many times. The thing is, it keeps getting assigned to a new array, which isn't good. I need it to be an object. If I can merge a dynamic amount of objects within the array, then I can easily convert it to an object but that's also been an issue.
this is the 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;
})
);
My sad attempt at giving it a go :(
let merged = response.map(x => {
let obj = { ...x };
return obj;
});
I've shortened a lot of the JSON but this is the gist of it.
[
{
"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 honestly just completely stumped. I don't know what I can do that will give me a dynamic solution.
Ideally, it would come out like this:
{
"posts": [
{
"author": "Zackery Turner",
"authorId": 12,
"id": 2,
"tags": [
"startups",
"tech",
"history"
]
},
{
"author": "Rylee Paul",
"authorId": 9,
"id": 1,
"tags": [
"tech",
"health"
]
}
Also, please I understand that I'm trying to do this dynamically. I wish it were as easy as hard coding it, but that's the solution that I see comes up when I browse similar threads on StackOverflow.