There are two sources from which I am retrieving data.
Once the data is fetched, I need to merge them into a single source.
Here's an example;
//data structure looks like:
//from url1
{
"data": [
{
"id": 7,
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8b5b1bbb0b9bdb4f6b4b9afabb7b698aabda9aabdabf6b1b6">[email protected]</a>",
"first_name": "Michael",
"last_name": "Lawson"
},
]
}
//from url2
{
"data": [
{
"id": 8,
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0cc...">">[email protected]</a>"
"first_name": "Lindsay",
"last_name": "Ferguson"
}
]
}
Upon merging, the desired outcome for the data:
{
"data": [
{
"id": 7,
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email_...">">[email protected]</a>"
"first_name": "Michael",
"last_name": "Lawson"
},
{
"id": 8,
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email_...">">[email protected]</a>"
"first_name": "Lindsay",
"last_name": "Ferguson"
}
]
}
Code snippet:
const url1 = "https://reqres.in/api/users";
const url2 = "https://reqres.in/api/users?page=2";
//store the data from url1
const data1 = fetch(url1).then(result=>{
return result.data;
})
//store the data from url2
const data2 = fetch(url2).then(result=>{
return result.data;
})
I attempted using Object.assign(), however it did not yield the expected results.
const data = Object.assign(data1, data2); // only retrieves data from data1, while data2 remains missing
console.log(data);