Search code examples
javascriptfetch-api

Can I loop the an array ob objects inside a fetch?


I´m trying to loop an array of objects inside a fetch. Is there a way I can do a loop inside a fetch or make the fetch return the array somehow?

This is what I´ve attempted:

fetch(url)
    .then(response => response.json())
    .then(data => 
        for (i = 0; i < data.length; i++) {
            console.log(data[i])
        })

Solution

  • You need to wrap .then(data) in curly braces

    fetch(url)
        .then(response => response.json())
        .then(data => {
            for (i = 0; i < data.length; i++) {
                console.log(data[i])
            }
         })