Using this code I’m getting a lot of JSON objects inside different arrays:
Code:
for (let i=1; i<=150;i++){
fetch(`A valid URL ${i}`)
.then(result => result.json())
.then(result => console.log(result.data.results))
}
How can I combine those arrays into one array?
2
Answers
If you don’t care about the order, you can push the results into another array:
However, you’d need to wait until all the
fetch
requests are finished, which you can do using async/await and Promise.all:If you do care about the order, then you need to return an array from the fetch request like so:
You can do it in one line like this: