Как перебрать объект json с вложенными объектами, чтобы получить нужные мне данные - PullRequest
0 голосов
/ 18 апреля 2019

У меня есть 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);
  }
}

Я застрял на том, как перебирать вложенные объекты.

1 Ответ

0 голосов
/ 18 апреля 2019

Вы должны обращаться к своим объектам следующим образом.

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);

   
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...