javascript - 如何在“for in”循环中获取对象?
问题描述
fetch('https://api.covid19india.org/state_district_wise.json')
.then(Response => Response.json())
.then(data => {
for(const prop in data){
console.log(prop)
console.log(prop.statecode) // undefined Why?, How can I access this?
}
})
需要帮助,prop.statecode 显示 undefined 为什么?,我怎样才能访问它?
解决方案
prop
这里是一个字符串。
您想要的是prop
用作数据中的键来获取对象并statecode
从中访问。
fetch('https://api.covid19india.org/state_district_wise.json')
.then(Response => Response.json())
.then(data => {
for(const prop in data){
console.log(prop)
console.log(data[prop].statecode) // undefined Why?, How can I access this?
}
})
推荐阅读
- php - 在 laravel 中设置时覆盖个人访问令牌过期
- javascript - 为什么回调没有返回浏览器上的调用 API
- python-3.x - 如何从python中的集合中打印随机变量
- angular - 如何在 Angular 应用程序中提供静态 gzip 压缩文件?
- angular - NPM INSTALL 在 Angular 项目中时出错
- javascript - 窗口 onbeforeunload 是否贬值?
- amazon-cloudformation - 参考从先前资源创建的 arn
- swift - 如何绘制线节点在相机中保持与 iPhone 中的 Measure App 相同的大小?
- excel - 如何防止 Excel 电子表格的免费分发
- react-native - 从反应原生打开外部应用程序