Presently, I have an async function that returns a JSON response in the form of an array containing two objects. Please refer to the screenshot.
https://i.sstatic.net/gCP8p.png How can I merge these objects to obtain:
[{resultCount: 100, results: Array(100)}]
I attempted the following code snippet:
var combinedResults = jsonResponses.map((acc, item) => {
acc.resultCount += item.resultCount;
acc.results += item.results;
return acc;
});
However, the outcome looked like this:
https://i.sstatic.net/BEEPy.png
Oh no!
If you could provide any assistance, it would be greatly appreciated!
export function getDoorstepsSongs () {
return async dispatch => {
const page1 = 'https://itunes.apple.com/search?country=us&media=music&limit=50&attribute=songTerm&term=door&sort=ratingIndex'
const page2 = 'https://itunes.apple.com/search?country=us&media=music&limit=50&offset=50&attribute=songTerm&term=door&sort=ratingIndex'
const jsonResponses = await Promise.all([
fetchJsonP(page1),
fetchJsonP(page2),
]).then(responses => {
return Promise.all(responses.map(res => res.json()))
}).catch(console.error)
console.time('processing songs')
//
// jsonResponses contains the results of two API requests
//
// 1. combine the results of these requests
// 2. sort the results with lib/sortSongs.js
//
const sortedSongs = sortSongs(combinedResults)
console.timeEnd('processing songs')
return dispatch({
type: 'GET_DOORSTEPS_SONGS_SUCCESS',
songs: sortedSongs,
})
}
}