我可以在 fetch 中循环数组 ob 对象吗?

问题描述 投票:0回答:1

我正在尝试在获取中循环对象数组。有没有办法可以在 fetch 中进行循环或使 fetch 以某种方式返回数组?

这就是我尝试过的:

fetch(url)
    .then(response => response.json())
    .then(data => 
        for (i = 0; i < data.length; i++) {
            console.log(data[i])
        })
javascript fetch-api
1个回答
3
投票

您需要将

.then(data)
用大括号括起来

fetch(url)
    .then(response => response.json())
    .then(data => {
        for (i = 0; i < data.length; i++) {
            console.log(data[i])
        }
     })
© www.soinside.com 2019 - 2024. All rights reserved.