У меня есть array объектов, и я пытаюсь создать новый объект, но без определенной пары key:value. Вот мой объект.

{
  "versionRoomPoolList": [{
      "pmsRoomType": ["CITY"],
      "versionRoomTypeAttributeList": [{
          "attributeDisplayId": 2,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "CV",
          "attributeDescription": "City View"
        },
        {
          "attributeDisplayId": 6,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "HF",
          "attributeDescription": "High Floor, 8th floor and above"
        },
        {
          "attributeDisplayId": 4,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "KN",
          "attributeDescription": "1 King Bed"
        },
        {
          "attributeDisplayId": 5,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "NE",
          "attributeDescription": "Near Elevator"
        },
        {
          "attributeDisplayId": 7,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "RF",
          "attributeDescription": "Mobile Lock"
        },
        {
          "attributeDisplayId": 3,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "SB",
          "attributeDescription": "Sofabed"
        },
        {
          "attributeDisplayId": 1,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "SU",
          "attributeDescription": "Alpine Guest Room"
        },
        {
          "attributeDisplayId": 8,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "TS",
          "attributeDescription": "Separate Shower and Bathtub"
        }
      ],
      "roomTypeDisplayId": 31,
      "roomTypeId": 21,
      "roomTypeStatus": "NOCHANGE",
      "roomPool": "CITY",
      "maxOccupancyStatus": "NOCHANGE",
      "newMaxOccupancy": 4,
      "currentMaxOccupancy": 4,
      "capacityStatus": "NOCHANGE",
      "newCapacity": 12,
      "currentCapacity": 12,
      "guaranteed": 9,
      "minAvailability": 1,
      "premium": false,
      "eliteAvailability": false,
      "isROH": false
    },
    {
      "versionRoomTypeList": [{
          "pmsRoomType": ["HOSP"],
          "versionRoomTypeAttributeList": [{
              "attributeDisplayId": 2,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "CV",
              "attributeDescription": "City View"
            },
            {
              "attributeDisplayId": 5,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "HF",
              "attributeDescription": "High Floor, 8th floor and above"
            },
            {
              "attributeDisplayId": 1,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "HS",
              "attributeDescription": "Hospitality Suite"
            },
            {
              "attributeDisplayId": 4,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "KN",
              "attributeDescription": "1 King Bed"
            },
            {
              "attributeDisplayId": 6,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "RF",
              "attributeDescription": "Mobile Lock"
            },
            {
              "attributeDisplayId": 3,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "SB",
              "attributeDescription": "Sofabed"
            },
            {
              "attributeDisplayId": 7,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "TS",
              "attributeDescription": "Separate Shower and Bathtub"
            }
          ],
          "roomTypeDisplayId": 34,
          "roomTypeId": 24,
          "roomTypeStatus": "NOCHANGE",
          "roomPool": "HOSP",
          "maxOccupancyStatus": "NOCHANGE",
          "newMaxOccupancy": 4,
          "currentMaxOccupancy": 4,
          "capacityStatus": "NOCHANGE",
          "newCapacity": 1,
          "currentCapacity": 1,
          "guaranteed": 1,
          "minAvailability": 1,
          "premium": true,
          "eliteAvailability": false,
          "isROH": true
        },
        {
          "pmsRoomType": ["HOSP"],
          "versionRoomTypeAttributeList": [{
              "attributeDisplayId": 2,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "CV",
              "attributeDescription": "City View"
            },
            {
              "attributeDisplayId": 6,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "HF",
              "attributeDescription": "High Floor, 8th floor and above"
            },
            {
              "attributeDisplayId": 1,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "HS",
              "attributeDescription": "Hospitality Suite"
            },
            {
              "attributeDisplayId": 4,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "KN",
              "attributeDescription": "1 King Bed"
            },
            {
              "attributeDisplayId": 5,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "NE",
              "attributeDescription": "Near Elevator"
            },
            {
              "attributeDisplayId": 7,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "RF",
              "attributeDescription": "Mobile Lock"
            },
            {
              "attributeDisplayId": 3,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "SB",
              "attributeDescription": "Sofabed"
            },
            {
              "attributeDisplayId": 8,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "TS",
              "attributeDescription": "Separate Shower and Bathtub"
            }
          ],
          "roomTypeDisplayId": 35,
          "roomTypeId": 25,
          "roomTypeStatus": "NOCHANGE",
          "roomPool": "HOSP",
          "maxOccupancyStatus": "NOCHANGE",
          "newMaxOccupancy": 4,
          "currentMaxOccupancy": 4,
          "capacityStatus": "NOCHANGE",
          "newCapacity": 1,
          "currentCapacity": 1,
          "guaranteed": 1,
          "minAvailability": 1,
          "premium": true,
          "eliteAvailability": false,
          "isROH": false
        }
      ],
      "roomPoolDisplayId": 4,
      "roomPoolStatus": "NOCHANGE",
      "roomPoolCode": "HOSP",
      "miosSRC": "YD",
      "sna": "N",
      "maxOccupancyStatus": "NOCHANGE",
      "newMaxOccupancy": 4,
      "currentMaxOccupancy": 4,
      "capacityStatus": "NOCHANGE",
      "newCapacity": 2,
      "currentCapacity": 2,
      "premium": true,
      "eliteAvailability": false,
      "excludeAlways": false,
      "excludeOverAuth": false,
      "propertySellOnly": false
    },
    {
      "pmsRoomType": ["HOSP"],
      "versionRoomTypeAttributeList": [{
          "attributeDisplayId": 2,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "CV",
          "attributeDescription": "City View"
        },
        {
          "attributeDisplayId": 5,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "HF",
          "attributeDescription": "High Floor, 8th floor and above"
        },
        {
          "attributeDisplayId": 1,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "HS",
          "attributeDescription": "Hospitality Suite"
        },
        {
          "attributeDisplayId": 4,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "KN",
          "attributeDescription": "1 King Bed"
        },
        {
          "attributeDisplayId": 6,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "RF",
          "attributeDescription": "Mobile Lock"
        },
        {
          "attributeDisplayId": 3,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "SB",
          "attributeDescription": "Sofabed"
        },
        {
          "attributeDisplayId": 7,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "TS",
          "attributeDescription": "Separate Shower and Bathtub"
        }
      ],
      "roomTypeDisplayId": 34,
      "roomTypeId": 24,
      "roomTypeStatus": "NOCHANGE",
      "roomPool": "HOSP",
      "maxOccupancyStatus": "NOCHANGE",
      "newMaxOccupancy": 4,
      "currentMaxOccupancy": 4,
      "capacityStatus": "NOCHANGE",
      "newCapacity": 1,
      "currentCapacity": 1,
      "guaranteed": 1,
      "minAvailability": 1,
      "premium": true,
      "eliteAvailability": false,
      "isROH": true
    }
  ]
}

Поэтому я пытаюсь удалить pmsRoomType в каждом из объектов.

Вот что я написал как часть более крупного метода, который я написал, чтобы попытаться решить эту проблему.

var data = {
  versionRoomPoolList: array.map(roompools, function(roompool) {
    roompool = lang.mixin({}, roompool);
    delete roompool.numAttributeChars;
    delete roompool.pmsRoomType;
    delete roompool.versionRoomTypeList[0].pmsRoomType;
    return roompool;
  })
};

Однако я продолжаю получать следующую ошибку:

Uncaught TypeError: Cannot read property '0' of undefined

Я предполагаю, что мне нужно написать другой метод .map, но я застрял на том, как это сделать, или это правильный путь?

0
Amen Ra 2 Май 2019 в 19:01

3 ответа

Лучший ответ

Ваши данные - это не массив, это объект с полем versionRoomPoolList.

Функция map вызывает предоставленную функцию обратного вызова один раз для каждого элемента в массиве по порядку и создает новый массив из результатов.

Я думаю, это то, что вы ищете: создайте новый объект (data) с versionRoomPoolList в качестве ключа и новый массив в качестве значения.

var rawData = {
  "versionRoomPoolList": [
    {
      "pmsRoomType": [
        "CITY"
      ],
      "versionRoomTypeAttributeList": [
        {
          "attributeDisplayId": 2,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "CV",
          "attributeDescription": "City View"
        },
        {
          "attributeDisplayId": 6,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "HF",
          "attributeDescription": "High Floor, 8th floor and above"
        },
        {
          "attributeDisplayId": 4,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "KN",
          "attributeDescription": "1 King Bed"
        },
        {
          "attributeDisplayId": 5,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "NE",
          "attributeDescription": "Near Elevator"
        },
        {
          "attributeDisplayId": 7,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "RF",
          "attributeDescription": "Mobile Lock"
        },
        {
          "attributeDisplayId": 3,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "SB",
          "attributeDescription": "Sofabed"
        },
        {
          "attributeDisplayId": 1,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "SU",
          "attributeDescription": "Alpine Guest Room"
        },
        {
          "attributeDisplayId": 8,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "TS",
          "attributeDescription": "Separate Shower and Bathtub"
        }
      ],
      "roomTypeDisplayId": 31,
      "roomTypeId": 21,
      "roomTypeStatus": "NOCHANGE",
      "roomPool": "CITY",
      "maxOccupancyStatus": "NOCHANGE",
      "newMaxOccupancy": 4,
      "currentMaxOccupancy": 4,
      "capacityStatus": "NOCHANGE",
      "newCapacity": 12,
      "currentCapacity": 12,
      "guaranteed": 9,
      "minAvailability": 1,
      "premium": false,
      "eliteAvailability": false,
      "isROH": false
    },
    {
      "versionRoomTypeList": [
        {
          "pmsRoomType": [
            "HOSP"
          ],
          "versionRoomTypeAttributeList": [
            {
              "attributeDisplayId": 2,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "CV",
              "attributeDescription": "City View"
            },
            {
              "attributeDisplayId": 5,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "HF",
              "attributeDescription": "High Floor, 8th floor and above"
            },
            {
              "attributeDisplayId": 1,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "HS",
              "attributeDescription": "Hospitality Suite"
            },
            {
              "attributeDisplayId": 4,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "KN",
              "attributeDescription": "1 King Bed"
            },
            {
              "attributeDisplayId": 6,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "RF",
              "attributeDescription": "Mobile Lock"
            },
            {
              "attributeDisplayId": 3,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "SB",
              "attributeDescription": "Sofabed"
            },
            {
              "attributeDisplayId": 7,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "TS",
              "attributeDescription": "Separate Shower and Bathtub"
            }
          ],
          "roomTypeDisplayId": 34,
          "roomTypeId": 24,
          "roomTypeStatus": "NOCHANGE",
          "roomPool": "HOSP",
          "maxOccupancyStatus": "NOCHANGE",
          "newMaxOccupancy": 4,
          "currentMaxOccupancy": 4,
          "capacityStatus": "NOCHANGE",
          "newCapacity": 1,
          "currentCapacity": 1,
          "guaranteed": 1,
          "minAvailability": 1,
          "premium": true,
          "eliteAvailability": false,
          "isROH": true
        },
        {
          "pmsRoomType": [
            "HOSP"
          ],
          "versionRoomTypeAttributeList": [
            {
              "attributeDisplayId": 2,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "CV",
              "attributeDescription": "City View"
            },
            {
              "attributeDisplayId": 6,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "HF",
              "attributeDescription": "High Floor, 8th floor and above"
            },
            {
              "attributeDisplayId": 1,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "HS",
              "attributeDescription": "Hospitality Suite"
            },
            {
              "attributeDisplayId": 4,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "KN",
              "attributeDescription": "1 King Bed"
            },
            {
              "attributeDisplayId": 5,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "NE",
              "attributeDescription": "Near Elevator"
            },
            {
              "attributeDisplayId": 7,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "RF",
              "attributeDescription": "Mobile Lock"
            },
            {
              "attributeDisplayId": 3,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "SB",
              "attributeDescription": "Sofabed"
            },
            {
              "attributeDisplayId": 8,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "TS",
              "attributeDescription": "Separate Shower and Bathtub"
            }
          ],
          "roomTypeDisplayId": 35,
          "roomTypeId": 25,
          "roomTypeStatus": "NOCHANGE",
          "roomPool": "HOSP",
          "maxOccupancyStatus": "NOCHANGE",
          "newMaxOccupancy": 4,
          "currentMaxOccupancy": 4,
          "capacityStatus": "NOCHANGE",
          "newCapacity": 1,
          "currentCapacity": 1,
          "guaranteed": 1,
          "minAvailability": 1,
          "premium": true,
          "eliteAvailability": false,
          "isROH": false
        }
      ],
      "roomPoolDisplayId": 4,
      "roomPoolStatus": "NOCHANGE",
      "roomPoolCode": "HOSP",
      "miosSRC": "YD",
      "sna": "N",
      "maxOccupancyStatus": "NOCHANGE",
      "newMaxOccupancy": 4,
      "currentMaxOccupancy": 4,
      "capacityStatus": "NOCHANGE",
      "newCapacity": 2,
      "currentCapacity": 2,
      "premium": true,
      "eliteAvailability": false,
      "excludeAlways": false,
      "excludeOverAuth": false,
      "propertySellOnly": false
    },
    {
      "pmsRoomType": [
        "HOSP"
      ],
      "versionRoomTypeAttributeList": [
        {
          "attributeDisplayId": 2,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "CV",
          "attributeDescription": "City View"
        },
        {
          "attributeDisplayId": 5,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "HF",
          "attributeDescription": "High Floor, 8th floor and above"
        },
        {
          "attributeDisplayId": 1,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "HS",
          "attributeDescription": "Hospitality Suite"
        },
        {
          "attributeDisplayId": 4,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "KN",
          "attributeDescription": "1 King Bed"
        },
        {
          "attributeDisplayId": 6,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "RF",
          "attributeDescription": "Mobile Lock"
        },
        {
          "attributeDisplayId": 3,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "SB",
          "attributeDescription": "Sofabed"
        },
        {
          "attributeDisplayId": 7,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "TS",
          "attributeDescription": "Separate Shower and Bathtub"
        }
      ],
      "roomTypeDisplayId": 34,
      "roomTypeId": 24,
      "roomTypeStatus": "NOCHANGE",
      "roomPool": "HOSP",
      "maxOccupancyStatus": "NOCHANGE",
      "newMaxOccupancy": 4,
      "currentMaxOccupancy": 4,
      "capacityStatus": "NOCHANGE",
      "newCapacity": 1,
      "currentCapacity": 1,
      "guaranteed": 1,
      "minAvailability": 1,
      "premium": true,
      "eliteAvailability": false,
      "isROH": true
    }
  ]
}

var data = {
  versionRoomPoolList: rawData.versionRoomPoolList.map(function(roompool) {
    roompool = Object.assign({}, roompool);
    delete roompool.numAttributeChars;
    delete roompool.pmsRoomType;
    // make sure `versionRoomTypeList` is existed
    roompool.versionRoomTypeList && delete roompool.versionRoomTypeList[0].pmsRoomType;
    return roompool;
  })
};
console.log(data)
1
hoangdv 2 Май 2019 в 16:51

Вы можете попробовать что-то вроде этого:

const data = {
  "versionRoomPoolList": [{
      "pmsRoomType": ["CITY"],
      "versionRoomTypeAttributeList": [{
          "attributeDisplayId": 2,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "CV",
          "attributeDescription": "City View"
        },
        {
          "attributeDisplayId": 6,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "HF",
          "attributeDescription": "High Floor, 8th floor and above"
        },
        {
          "attributeDisplayId": 4,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "KN",
          "attributeDescription": "1 King Bed"
        },
        {
          "attributeDisplayId": 5,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "NE",
          "attributeDescription": "Near Elevator"
        },
        {
          "attributeDisplayId": 7,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "RF",
          "attributeDescription": "Mobile Lock"
        },
        {
          "attributeDisplayId": 3,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "SB",
          "attributeDescription": "Sofabed"
        },
        {
          "attributeDisplayId": 1,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "SU",
          "attributeDescription": "Alpine Guest Room"
        },
        {
          "attributeDisplayId": 8,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "TS",
          "attributeDescription": "Separate Shower and Bathtub"
        }
      ],
      "roomTypeDisplayId": 31,
      "roomTypeId": 21,
      "roomTypeStatus": "NOCHANGE",
      "roomPool": "CITY",
      "maxOccupancyStatus": "NOCHANGE",
      "newMaxOccupancy": 4,
      "currentMaxOccupancy": 4,
      "capacityStatus": "NOCHANGE",
      "newCapacity": 12,
      "currentCapacity": 12,
      "guaranteed": 9,
      "minAvailability": 1,
      "premium": false,
      "eliteAvailability": false,
      "isROH": false
    },
    {
      "versionRoomTypeList": [{
          "pmsRoomType": ["HOSP"],
          "versionRoomTypeAttributeList": [{
              "attributeDisplayId": 2,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "CV",
              "attributeDescription": "City View"
            },
            {
              "attributeDisplayId": 5,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "HF",
              "attributeDescription": "High Floor, 8th floor and above"
            },
            {
              "attributeDisplayId": 1,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "HS",
              "attributeDescription": "Hospitality Suite"
            },
            {
              "attributeDisplayId": 4,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "KN",
              "attributeDescription": "1 King Bed"
            },
            {
              "attributeDisplayId": 6,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "RF",
              "attributeDescription": "Mobile Lock"
            },
            {
              "attributeDisplayId": 3,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "SB",
              "attributeDescription": "Sofabed"
            },
            {
              "attributeDisplayId": 7,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "TS",
              "attributeDescription": "Separate Shower and Bathtub"
            }
          ],
          "roomTypeDisplayId": 34,
          "roomTypeId": 24,
          "roomTypeStatus": "NOCHANGE",
          "roomPool": "HOSP",
          "maxOccupancyStatus": "NOCHANGE",
          "newMaxOccupancy": 4,
          "currentMaxOccupancy": 4,
          "capacityStatus": "NOCHANGE",
          "newCapacity": 1,
          "currentCapacity": 1,
          "guaranteed": 1,
          "minAvailability": 1,
          "premium": true,
          "eliteAvailability": false,
          "isROH": true
        },
        {
          "pmsRoomType": ["HOSP"],
          "versionRoomTypeAttributeList": [{
              "attributeDisplayId": 2,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "CV",
              "attributeDescription": "City View"
            },
            {
              "attributeDisplayId": 6,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "HF",
              "attributeDescription": "High Floor, 8th floor and above"
            },
            {
              "attributeDisplayId": 1,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "HS",
              "attributeDescription": "Hospitality Suite"
            },
            {
              "attributeDisplayId": 4,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "KN",
              "attributeDescription": "1 King Bed"
            },
            {
              "attributeDisplayId": 5,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "NE",
              "attributeDescription": "Near Elevator"
            },
            {
              "attributeDisplayId": 7,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "RF",
              "attributeDescription": "Mobile Lock"
            },
            {
              "attributeDisplayId": 3,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "SB",
              "attributeDescription": "Sofabed"
            },
            {
              "attributeDisplayId": 8,
              "attributeStatus": "NOCHANGE",
              "attributeCode": "TS",
              "attributeDescription": "Separate Shower and Bathtub"
            }
          ],
          "roomTypeDisplayId": 35,
          "roomTypeId": 25,
          "roomTypeStatus": "NOCHANGE",
          "roomPool": "HOSP",
          "maxOccupancyStatus": "NOCHANGE",
          "newMaxOccupancy": 4,
          "currentMaxOccupancy": 4,
          "capacityStatus": "NOCHANGE",
          "newCapacity": 1,
          "currentCapacity": 1,
          "guaranteed": 1,
          "minAvailability": 1,
          "premium": true,
          "eliteAvailability": false,
          "isROH": false
        }
      ],
      "roomPoolDisplayId": 4,
      "roomPoolStatus": "NOCHANGE",
      "roomPoolCode": "HOSP",
      "miosSRC": "YD",
      "sna": "N",
      "maxOccupancyStatus": "NOCHANGE",
      "newMaxOccupancy": 4,
      "currentMaxOccupancy": 4,
      "capacityStatus": "NOCHANGE",
      "newCapacity": 2,
      "currentCapacity": 2,
      "premium": true,
      "eliteAvailability": false,
      "excludeAlways": false,
      "excludeOverAuth": false,
      "propertySellOnly": false
    },
    {
      "pmsRoomType": ["HOSP"],
      "versionRoomTypeAttributeList": [{
          "attributeDisplayId": 2,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "CV",
          "attributeDescription": "City View"
        },
        {
          "attributeDisplayId": 5,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "HF",
          "attributeDescription": "High Floor, 8th floor and above"
        },
        {
          "attributeDisplayId": 1,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "HS",
          "attributeDescription": "Hospitality Suite"
        },
        {
          "attributeDisplayId": 4,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "KN",
          "attributeDescription": "1 King Bed"
        },
        {
          "attributeDisplayId": 6,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "RF",
          "attributeDescription": "Mobile Lock"
        },
        {
          "attributeDisplayId": 3,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "SB",
          "attributeDescription": "Sofabed"
        },
        {
          "attributeDisplayId": 7,
          "attributeStatus": "NOCHANGE",
          "attributeCode": "TS",
          "attributeDescription": "Separate Shower and Bathtub"
        }
      ],
      "roomTypeDisplayId": 34,
      "roomTypeId": 24,
      "roomTypeStatus": "NOCHANGE",
      "roomPool": "HOSP",
      "maxOccupancyStatus": "NOCHANGE",
      "newMaxOccupancy": 4,
      "currentMaxOccupancy": 4,
      "capacityStatus": "NOCHANGE",
      "newCapacity": 1,
      "currentCapacity": 1,
      "guaranteed": 1,
      "minAvailability": 1,
      "premium": true,
      "eliteAvailability": false,
      "isROH": true
    }
  ]
}

const myFunction = data => ({
  ...data,
  versionRoomPoolList: data.versionRoomPoolList.filter(i => {
    delete i.pmsRoomType;
    return i;
  }),
});

console.log(myFunction(data))
0
Jose A. Ayllón 3 Май 2019 в 15:06

Ваш код в основном правильный, и, как Феликс Клинг прокомментировал ваше сообщение, вам просто нужно защититься от несуществующих свойств. Это может быть так просто, как:

if (roompool.versionRoomTypeList && roompool.versionRoomTypeList.length) {
  // delete
}

Тем не менее ... Я подозреваю, что у вас просто опечатка, и вы должны изменить versionRoomTypeList на versionRoomTypeAttributeList :-P

0
vassiliskrikonis 3 Май 2019 в 16:38