Javascript 为什么我获取的JSON使用[object]而不是数组?
我想从Fetch API JSON中整理一些数据。使用Postman GET很好,但是当我使用console.log()打印结果时,它会打印出[Array]。此外,我无法访问代码中的“foodNutrients”数组 当我使用Postman GET输入url时Javascript 为什么我获取的JSON使用[object]而不是数组?,javascript,json,fetch,Javascript,Json,Fetch,我想从Fetch API JSON中整理一些数据。使用Postman GET很好,但是当我使用console.log()打印结果时,它会打印出[Array]。此外,我无法访问代码中的“foodNutrients”数组 当我使用Postman GET输入url时 "foodNutrients": [ { "nutrientId": 1051, &q
"foodNutrients": [
{
"nutrientId": 1051,
"nutrientName": "Water",
"nutrientNumber": "255",
"unitName": "G",
"derivationCode": "A",
"derivationDescription": "Analytical",
"value": 0E-8
},
{
"nutrientId": 1003,
"nutrientName": "Protein",
"nutrientNumber": "203",
"unitName": "G",
"derivationCode": "A",
"derivationDescription": "Analytical",
"value": 24.40000000
},
]
但是当我使用Fetch和console.log()时
我的代码
const dataFilter= (response) => {
console.log(response)
async function getData() {
fetch('https://api.nal.usda.gov/fdc/v1/foods/search?api_key=DEMO_KEY&query=0')
.then(response => response.json())
.then(response => dataFilter(response))
}
getData()
感谢您的帮助:)这正是控制台表达“这里有一个大数组”的方式 这是一种性能特性,它通过简化数据视图来工作
例如,您可以将
console.log(response)
更改为response.foodNutrients.forEach(nut=>console.log(nut))
将显示数组中的对象。这正是控制台表达“这里有一个大数组”的方式
这是一种性能特性,它通过简化数据视图来工作
例如,您可以将
console.log(response)
更改为response.foodNutrients.forEach(nut=>console.log(nut))
将显示数组中的对象。它是postman和console.log中的一个对象。我不知道您到底想问什么,这两者都是一个数组当您执行console.log时会得到什么(JSON.stringify(response,null,2))
?它是postman和console.log中的一个对象我不知道你到底想问什么这两者都是一个数组当你执行console.log(JSON.stringify(response,null,2))时会得到什么
?啊。我也应该能够访问它里面的数据,对吗?啊。我也应该能够访问它里面的数据,对吗?
const dataFilter= (response) => {
console.log(response)
async function getData() {
fetch('https://api.nal.usda.gov/fdc/v1/foods/search?api_key=DEMO_KEY&query=0')
.then(response => response.json())
.then(response => dataFilter(response))
}
getData()