javascript - 访问某个 json 对象
问题描述
所以我一直在试图通过 JS 中的 for 循环访问某个元素,我正在画一个空白。我有带有 15 个元素的 JSON 对象(下面的一个示例。我正在尝试访问“phones”.phone_number 元素但未定义。下面也是我的 for 循环。
现在我只是控制台记录所有元素。前两个,.name 和 address 回来查找,但它的手机没有返回结果。
for(var i=0; i < json.business.length; i++ ){
console.log(json.business[i].name);
console.log(json.business[i].found_at_address.street_line_1);
console.log(json.business[i].phones[i].phone_number);
}
{
"count_business": 15,
"business": [
{
"id": "Business.7146cd10-735f-4710-b145-93b97fe45e07",
"name": "Grace Christian Fellowship",
"industry": [
"Religious, Grantmaking, Civic, Professional, and Similar Organizations"
],
"found_at_address": {
"id": "Location.5c80fbd1-5e5a-4356-9725-5492e3942091",
"location_type": "Address",
"street_line_1": "210 2nd St",
"street_line_2": null,
"city": "Mounds",
"postal_code": "62964",
"zip4": "1144",
"state_code": "IL",
"country_code": "US",
"lat_long": {
"latitude": 37.113098,
"longitude": -89.200842,
"accuracy": "RoofTop"
},
"is_active": true,
"delivery_point": "POBoxThrowback",
"link_to_business_start_date": "2016-11-19",
"link_to_business_end_date": null
},
"current_addresses": [
{
"id": "Location.5c80fbd1-5e5a-4356-9725-5492e3942091",
"location_type": "Address",
"street_line_1": "210 2nd St",
"street_line_2": null,
"city": "Mounds",
"postal_code": "62964",
"zip4": "1144",
"state_code": "IL",
"country_code": "US",
"lat_long": {
"latitude": 37.113098,
"longitude": -89.200842,
"accuracy": "RoofTop"
},
"is_active": true,
"delivery_point": "POBoxThrowback",
"link_to_business_start_date": "2016-11-19"
}
],
"historical_addresses": [],
"phones": [
{
"id": "Phone.46c16fef-a2e1-4b08-cfe3-bc7128b6e19a",
"phone_number": "+16187459424",
"line_type": "Landline"
}
],
"associated_people": [],
"associated_businesses": [
{
"id": "Business.e43f1c0d-ecec-42da-ad1c-76badfdf2dcf",
"name": "Usda Rural Development",
"industry": [
"Administration of Housing Programs, Urban Planning, and Community Development"
],
"relation": "Household"
}
]
},
解决方案
遍历phones
数组时需要创建一个单独的循环。由于json.business.length
是 15,i
将一直到 14。但是,phones.length
只有 1,所以phones[14]
会抛出错误。
使用下面的代码:
for(var i=0; i < json.business.length; i++ ){
console.log(json.business[i].name);
console.log(json.business[i].found_at_address.street_line_1);
for (var x = 0; x < json.business[i].phones.length; x++) {
console.log(json.business[i].phones[x].phone_number);
}
}
注意:更好的方法是使用.forEach()
循环。这基本上循环整个数组,比for
循环容易得多。
json.business.forEach(business => {
console.log(business.name);
console.log(business.found_at_address.street_line_1);
business.forEach(phones => {
console.log(phones.phone_number);
}
})
.forEach()
循环基本上做了循环的for
作用,但在这种情况下,business
将是json.business[i]
推荐阅读
- sonarqube - SonarQube:项目验证失败,模块已经是项目的一部分
- node.js - npm 审计未识别在较低级别解决的问题
- angular - 如何获取 JSON 内部对象的数据?
- cordova - Phonegap/Cordova:用于显示 PDF 并为 Linkhandler (iOS / Android) 提供功能的插件
- https - wkhtmltopdf 无法在 laravel 上获取 https 图像
- android - React Native - 模态显示在不同的 FlatList 屏幕上
- javascript - Jasmine:TypeError:无法读取未定义的属性“toLowerCase”
- python - Chromedriver 在路径中工作但不使用 selenium (python)
- apache2 - apache2ctl DUMP_RUN_CFG 和 DUMP_INCLUDES 失败
- javascript - 条形图中的Chart.js颜色未显示