Combining multiple json fetch requests into one array

Have been working on a project where I need to combine multiple JSON files into one array. The code below will take an array of URLs and combine them into one array.

Once all the fetch requests have been completed and converted to json you’ll have an equal number of arrays. Using Array.prototype.flat() combines all the arrays into a single array.

let finalResult;
const urls = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
Promise.all(
urls.map(url =>
    fetch('json/' + url + '.json')
        .then(e => e.json())
    )
).then(data => {
    finalResult = data.flat();
});
Matt Busche's Picture

About Matt Busche

Software Engineer and Wheel of Fortune Expert If this article helped you, please consider buying me a book.

Des Moines, IA https://www.mrbusche.com