我正在尝试在获取中循环对象数组。有没有办法可以在 fetch 中进行循环或使 fetch 以某种方式返回数组?
这就是我尝试过的:
fetch(url)
.then(response => response.json())
.then(data =>
for (i = 0; i < data.length; i++) {
console.log(data[i])
})
您需要将
.then(data)
用大括号括起来
fetch(url)
.then(response => response.json())
.then(data => {
for (i = 0; i < data.length; i++) {
console.log(data[i])
}
})