javascript - 具有重复属性名称的嵌套 JSON 数组访问
问题描述
如何访问 JS 中的“屋顶绝缘”数组值?甚至可以使用重复的属性名称吗?
我尝试了 productListing['Roofing Insulation']['Roofing Insulation'] 和更多类型的语法,但没有成功。我知道重复是不好的做法,但这是我必须要做的一切......
{
"storeServices":{
"Local Services":[
"Rooftop",
"EPDM",
"Next-Day Delivery Available"
],
"National Services":[
"Covered",
"Window",
"Job Site Signs"
]
},
"productListing":{
"Roofing Insulation":{
"Roofing Insulation":[
"Celotex",
"Dow",
"Foamular",
"Johns Manville",
"Pactiv",
"Tyvek"
]
},
"Other Products":{
"Other Products":[
"Caulks/Sealants",
"Shutters",
"Tools & Equipment",
"Engineered Lumber",
"Roofing & Siding Accessories"
]
},
"Low Slope Roofing":{
"Low Slope Roofing":[
"Johns Manville",
"Mule-Hide Products",
"Versico"
]
},
"Steep Slope Roofing":{
"Concrete & Clay Roof Tiles":[
"Vande Hey Raleigh"
],
"Asphalt Shingles":[
"CertainTeed",
"GAF",
"Owens Corning"
]
},
"Windows & Doors":{
"Replacement Windows":[
"Pella"
],
"New Construction Windows":[
"Pella"
],
"Exterior Doors":[
"Pella"
],
"Skylights":[
"Velux"
]
},
"Siding":{
"Vinyl Siding":[
"Mastic Home Exteriors"
]
}
},
"branchDetails":{
"branchNumber":"1"
}
}
解决方案
除非我们看到您尝试过的内容和您的代码,否则我们无能为力。如果您像下面这样访问它,它应该可以工作:
obj.productListing['Roofing Insulation']['Roofing Insulation']
下面是一个示例供您展示:
var obj = {
"storeServices": {
"Local Services": [
"Rooftop",
"EPDM",
"Next-Day Delivery Available"
],
"National Services": [
"Covered",
"Window",
"Job Site Signs"
]
},
"productListing": {
"Roofing Insulation": {
"Roofing Insulation": [
"Celotex",
"Dow",
"Foamular",
"Johns Manville",
"Pactiv",
"Tyvek"
]
},
"Other Products": {
"Other Products": [
"Caulks/Sealants",
"Shutters",
"Tools & Equipment",
"Engineered Lumber",
"Roofing & Siding Accessories"
]
},
"Low Slope Roofing": {
"Low Slope Roofing": [
"Johns Manville",
"Mule-Hide Products",
"Versico"
]
},
"Steep Slope Roofing": {
"Concrete & Clay Roof Tiles": [
"Vande Hey Raleigh"
],
"Asphalt Shingles": [
"CertainTeed",
"GAF",
"Owens Corning"
]
},
"Windows & Doors": {
"Replacement Windows": [
"Pella"
],
"New Construction Windows": [
"Pella"
],
"Exterior Doors": [
"Pella"
],
"Skylights": [
"Velux"
]
},
"Siding": {
"Vinyl Siding": [
"Mastic Home Exteriors"
]
}
},
"branchDetails": {
"branchNumber": "1"
}
};
document.getElementById("demo").innerHTML = obj.productListing['Roofing Insulation']['Roofing Insulation'];
<p id="demo"></p>
推荐阅读
- shopify - 如何在任何页面中创建和调用自定义块
- html - XAMPP 不加载 CSS 文件
- firebase - Flutter Firestore Stem Data 想要在 StaggeredGridView 中显示
- python - 关于 min() 函数中“key”的问题
- python - 将 pandas 列中的值分解为 bin
- amazon-elb - 将实例注册到已附加实例的 ELB 或目标组
- javascript - ref数组在反应中返回0长度
- c# - C# DSM(数据源管理器)使用NTWAIN库不显示
- sql - 如何在删除 SQL 关系数据库中的产品时自动删除所有关联的组件?
- amazon-web-services - AWS Codebuild:YAML_FILE_ERROR 消息:预期变量为映射类型: