首页 > 解决方案 > 谁在 JavaScript 中映射嵌套对象并删除键值对

问题描述

我有一个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方法,但我被困在如何做到这一点上,还是那是正确的方法?

标签: javascriptarraysjson

解决方案


你的数据不是一个数组,它是一个带有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)

推荐阅读