javascript - 如何遍历具有嵌套对象的 json 对象以获取所需的数据
问题描述
我有一个json
对象,我需要从其中的嵌套对象中获取两条数据。
这是json
对象
{
"versionRoomPoolList": [{
"roomPoolDisplayId": 1,
"roomPoolStatus": "NOCHANGE",
"roomPoolCode": "GENR",
"miosSRC": "YA",
"sna": "N",
"maxOccupancyStatus": "NOCHANGE",
"newMaxOccupancy": 2,
"currentMaxOccupancy": 2,
"capacityStatus": "NOCHANGE",
"newCapacity": 0,
"currentCapacity": 0,
"excludeAlways": false,
"excludeOverAuth": false,
"propertySellOnly": false,
"versionRoomTypeList": [{
"roomTypeDisplayOrderId": 1,
"roomTypeId": 1,
"roomTypeStatus": "NOCHANGE",
"roomPool": "GENR",
"maxOccupancyStatus": "NOCHANGE",
"newMaxOccupancy": 2,
"currentMaxOccupancy": 2,
"capacityStatus": "NOCHANGE",
"newCapacity": 0,
"currentCapacity": 0,
"guaranteed": 0,
"minAvailability": null,
"premium": false,
"eliteAvailability": false,
"pmsRoomType": null,
"isROH": false,
"versionRoomTypeAttributeList": [{
"attributeDisplayId": 1,
"attributeStatus": "NOCHANGE",
"attributeCode": "GU",
"attributeDescription": "Guest Room"
}]
}]
},
{
"roomPoolDisplayId": 2,
"roomPoolStatus": "NOCHANGE",
"roomPoolCode": "DLUX",
"miosSRC": "YB",
"sna": "N",
"maxOccupancyStatus": "NOCHANGE",
"newMaxOccupancy": 2,
"currentMaxOccupancy": 2,
"capacityStatus": "NOCHANGE",
"newCapacity": 238,
"currentCapacity": 238,
"excludeAlways": false,
"excludeOverAuth": false,
"propertySellOnly": false,
"versionRoomTypeList": [{
"roomTypeDisplayOrderId": 2,
"roomTypeId": 20,
"roomTypeStatus": "NOCHANGE",
"roomPool": "DLUX",
"maxOccupancyStatus": "NOCHANGE",
"newMaxOccupancy": 3,
"currentMaxOccupancy": 3,
"capacityStatus": "NOCHANGE",
"newCapacity": 6,
"currentCapacity": 6,
"guaranteed": 4,
"minAvailability": 1,
"premium": false,
"eliteAvailability": false,
"pmsRoomType": null,
"isROH": false,
"versionRoomTypeAttributeList": [{
"attributeDisplayId": 1,
"attributeStatus": "NOCHANGE",
"attributeCode": "GU",
"attributeDescription": "Guest Room"
},
{
"attributeDisplayId": 4,
"attributeStatus": "NOCHANGE",
"attributeCode": "HF",
"attributeDescription": "High Floor, 11th floor and above"
},
{
"attributeDisplayId": 3,
"attributeStatus": "NOCHANGE",
"attributeCode": "KN",
"attributeDescription": "1 King Bed"
},
{
"attributeDisplayId": 2,
"attributeStatus": "NOCHANGE",
"attributeCode": "SB",
"attributeDescription": "Sofabed"
},
{
"attributeDisplayId": 5,
"attributeStatus": "NOCHANGE",
"attributeCode": "SE",
"attributeDescription": "Separate Shower and Bathtub"
}
]
}]
}
]
}
我试图获取两条数据并将其放入数据网格的列中。第一个值来自键versionRoomPoolList.roomPoolCode
和versionRoomPoolList.versionRoomTypeList.versionRoomTypeAttributeList.attributeCode
我正在尝试使用for loops
这里来做到这一点,这是我到目前为止所拥有的:
var v2 = versionRoomPools;
roomPool,
roomTypes,
roomType,
i,
k = [],
j;
for (i = 0;
(roomPool = v2.versionRoomPoolList[i]); i++) {
k.push(roomPool);
roomTypes = roomPool.versionRoomTypeList;
for (j = 0;
(roomType = roomTypes[j]); j++) {
k.push(roomType);
}
}
我被困在如何迭代嵌套对象上。
解决方案
您应该像这样访问您的对象。
console.log(data.versionRoomPoolList[0].roomPoolCode); // roomPoolCode
data.versionRoomPoolList[0].versionRoomTypeList[0].versionRoomTypeAttributeList.forEach(v => {
console.log(v)
})
我们还可以遍历所有键,如果它是嵌套对象,则调用该函数。
function search(o) {
Object.keys(o).forEach(function (k) {
console.log('Searched', o, o[k]);
if (o[k] !== null && typeof o[k] === 'object') {
search(o[k]);
return;
}
console.log(o[k] === o['roomPoolCode']); // Logic
if (o[k] === o['roomPoolCode'] {
// Do whatever with it
}
return o[k];
});
}
let data = {
"versionRoomPoolList": [{
"roomPoolDisplayId": 1,
"roomPoolStatus": "NOCHANGE",
"roomPoolCode": "GENR",
"miosSRC": "YA",
"sna": "N",
"maxOccupancyStatus": "NOCHANGE",
"newMaxOccupancy": 2,
"currentMaxOccupancy": 2,
"capacityStatus": "NOCHANGE",
"newCapacity": 0,
"currentCapacity": 0,
"excludeAlways": false,
"excludeOverAuth": false,
"propertySellOnly": false,
"versionRoomTypeList": [{
"roomTypeDisplayOrderId": 1,
"roomTypeId": 1,
"roomTypeStatus": "NOCHANGE",
"roomPool": "GENR",
"maxOccupancyStatus": "NOCHANGE",
"newMaxOccupancy": 2,
"currentMaxOccupancy": 2,
"capacityStatus": "NOCHANGE",
"newCapacity": 0,
"currentCapacity": 0,
"guaranteed": 0,
"minAvailability": null,
"premium": false,
"eliteAvailability": false,
"pmsRoomType": null,
"isROH": false,
"versionRoomTypeAttributeList": [{
"attributeDisplayId": 1,
"attributeStatus": "NOCHANGE",
"attributeCode": "GU",
"attributeDescription": "Guest Room"
}]
}]
},
{
"roomPoolDisplayId": 2,
"roomPoolStatus": "NOCHANGE",
"roomPoolCode": "DLUX",
"miosSRC": "YB",
"sna": "N",
"maxOccupancyStatus": "NOCHANGE",
"newMaxOccupancy": 2,
"currentMaxOccupancy": 2,
"capacityStatus": "NOCHANGE",
"newCapacity": 238,
"currentCapacity": 238,
"excludeAlways": false,
"excludeOverAuth": false,
"propertySellOnly": false,
"versionRoomTypeList": [{
"roomTypeDisplayOrderId": 2,
"roomTypeId": 20,
"roomTypeStatus": "NOCHANGE",
"roomPool": "DLUX",
"maxOccupancyStatus": "NOCHANGE",
"newMaxOccupancy": 3,
"currentMaxOccupancy": 3,
"capacityStatus": "NOCHANGE",
"newCapacity": 6,
"currentCapacity": 6,
"guaranteed": 4,
"minAvailability": 1,
"premium": false,
"eliteAvailability": false,
"pmsRoomType": null,
"isROH": false,
"versionRoomTypeAttributeList": [{
"attributeDisplayId": 1,
"attributeStatus": "NOCHANGE",
"attributeCode": "GU",
"attributeDescription": "Guest Room"
},
{
"attributeDisplayId": 4,
"attributeStatus": "NOCHANGE",
"attributeCode": "HF",
"attributeDescription": "High Floor, 11th floor and above"
},
{
"attributeDisplayId": 3,
"attributeStatus": "NOCHANGE",
"attributeCode": "KN",
"attributeDescription": "1 King Bed"
},
{
"attributeDisplayId": 2,
"attributeStatus": "NOCHANGE",
"attributeCode": "SB",
"attributeDescription": "Sofabed"
},
{
"attributeDisplayId": 5,
"attributeStatus": "NOCHANGE",
"attributeCode": "SE",
"attributeDescription": "Separate Shower and Bathtub"
}
]
}]
}
]
}
function search(o,) {
Object.keys(o).forEach(function (k) {
console.log('Searched', o, o[k]);
if (o[k] !== null && typeof o[k] === 'object') {
search(o[k]);
return;
}
console.log(o[k] === o['roomPoolCode']); // o Something
return o[k];
});
}
search(data);
推荐阅读
- linux - 无法在 shell 脚本中打印公共 ip
- swift - 从字符串中删除标点符号,Swift 中的破折号除外
- reactjs - 如果 api 响应需要更长的时间,则在 UI 中显示超时错误
- phaser-framework - fire from a weapon of project motion using phaser 2.5.0
- android - 在模拟器下运行金鱼内核的分段错误
- python - Python pandas 在加载 csv 文件时错过了一些行尾字符
- c - queue array based implementation
- swift - NSView 和 CALayer
- javascript - React-Redux Chrome 扩展程序收到操作类型未定义的错误
- javascript - 访问 JS if/else 的多个数组元素