在JavaScript中映射嵌套对象并删除键值对的用户

在JavaScript中映射嵌套对象并删除键值对的用户,javascript,arrays,json,Javascript,Arrays,Json,我有一个对象数组,我正试图创建一个新对象,但其中没有一个特定的键:值对。这是我的目标 { versionRoomPoolList:[{ pmsRoomType:[城市], versionRoomTypeAttributeList:[{ attributeDisplayId:2, 属性status:NOCHANGE, 属性代码:CV, 属性说明:城市视图 }, { 属性显示ID:6, 属性status:NOCHANGE, 属性代码:HF, 属性描述:高层、8层及以上 }, { 属性显示ID:4,

我有一个对象数组,我正试图创建一个新对象,但其中没有一个特定的键:值对。这是我的目标

{ versionRoomPoolList:[{ pmsRoomType:[城市], versionRoomTypeAttributeList:[{ attributeDisplayId:2, 属性status:NOCHANGE, 属性代码:CV, 属性说明:城市视图 }, { 属性显示ID:6, 属性status:NOCHANGE, 属性代码:HF, 属性描述:高层、8层及以上 }, { 属性显示ID:4, 属性status:NOCHANGE, 属性代码:KN, 属性描述:1张大床 }, { 属性显示ID:5, 属性status:NOCHANGE, 属性代码:NE, 属性说明:电梯附近 }, { 属性显示ID:7, 属性status:NOCHANGE, 属性代码:RF, 属性说明:移动锁 }, { attributeDisplayId:3, 属性status:NOCHANGE, 属性代码:SB, 属性说明:沙发床 }, { attributeDisplayId:1, 属性status:NOCHANGE, 属性代码:苏, 属性描述:阿尔卑斯酒店客房 }, { 属性显示ID:8, 属性status:NOCHANGE, 属性代码:TS, 属性描述:独立淋浴和浴缸 } ], roomTypeDisplayId:31, 房间类型ID:21, roomTypeStatus:无更改, 房间池:城市, 最大职业状态:无变化, 新入住率:4, 当前最大入住率:4, 电容状态:无变化, 新增容量:12, 当前容量:12, 保证:9, 可利用率:1, 溢价:错, 精英可用性:错误, 伊斯罗:错 }, { versionRoomTypeList:[{ pmsRoomType:[HOSP], versionRoomTypeAttributeList:[{ attributeDisplayId:2, 属性status:NOCHANGE, 属性代码:CV, 属性说明:城市视图 }, { 属性显示ID:5, 属性status:NOCHANGE, 属性代码:HF, 属性描述:高层、8层及以上 }, { attributeDisplayId:1, 属性status:NOCHANGE, 属性代码:HS, 属性描述:酒店套房 }, { 属性显示ID:4, 属性status:NOCHANGE, 属性代码:KN, 属性描述:1张大床 }, { 属性显示ID:6, 属性status:NOCHANGE, 属性代码:RF, 属性说明:移动锁 }, { attributeDisplayId:3, 属性status:NOCHANGE, 属性代码:SB, 属性说明:沙发床 }, { 属性显示ID:7, 属性status:NOCHANGE, 属性代码:TS, 属性描述:独立淋浴和浴缸 } ], roomTypeDisplayId:34, 房间类型ID:24, roomTypeStatus:无更改, roomPool:HOSP, 最大职业状态:无变化, 新入住率:4, 当前最大入住率:4, 电容状态:无变化, 新增产能:1, 当前容量:1, 保证:1,, 可利用率:1, 溢价:是的, 精英可用性:错误, 伊斯罗:是的 }, { pmsRoomType:[HOSP], versionRoomTypeAttributeList:[{ attributeDisplayId:2, 属性status:NOCHANGE, 属性代码:CV, 属性说明:城市视图 }, { 属性显示ID:6, 属性status:NOCHANGE, 属性代码:HF, 属性描述:高层、8层及以上 }, { attributeDisplayId:1, 属性status:NOCHANGE, 属性代码:HS, 属性描述:酒店套房 }, { 属性显示ID:4, 属性status:NOCHANGE, 属性代码:KN, 属性描述:1张大床 }, { 属性显示ID:5, 属性status:NOCHANGE, 属性代码:NE, 属性说明: 近电梯 }, { 属性显示ID:7, 属性status:NOCHANGE, 属性代码:RF, 属性说明:移动锁 }, { attributeDisplayId:3, 属性status:NOCHANGE, 属性代码:SB, 属性说明:沙发床 }, { 属性显示ID:8, 属性status:NOCHANGE, 属性代码:TS, 属性描述:独立淋浴和浴缸 } ], roomTypeDisplayId:35, 房间类型ID:25, roomTypeStatus:无更改, roomPool:HOSP, 最大职业状态:无变化, 新入住率:4, 当前最大入住率:4, 电容状态:无变化, 新增产能:1, 当前容量:1, 保证:1,, 可利用率:1, 溢价:是的, 精英可用性:错误, 伊斯罗:错 } ], roomPoolDisplayId:4, RoomPool状态:无更改, 房间代码:HOSP, Miosrc:YD, sna:N, 最大职业状态:无变化, 新入住率:4, 当前最大入住率:4, 电容状态:无变化, 新增产能:2, 当前容量:2, 溢价:是的, 精英可用性:错误, 总是:假, 排他性:假, 仅属性:false }, { pmsRoomType:[HOSP], versionRoomTypeAttributeList:[{ attributeDisplayId:2, 属性status:NOCHANGE, 属性代码:CV, 属性说明:城市视图 }, { 属性显示ID:5, 属性status:NOCHANGE, 属性代码:HF, 属性描述:高层、8层及以上 }, { attributeDisplayId:1, 属性status:NOCHANGE, 属性代码:HS, 属性描述:酒店套房 }, { 属性显示ID:4, 属性status:NOCHANGE, 属性代码:KN, 属性描述:1张大床 }, { 属性显示ID:6, 属性status:NOCHANGE, 属性代码:RF, 属性说明:移动锁 }, { attributeDisplayId:3, 属性status:NOCHANGE, 属性代码:SB, 属性说明:沙发床 }, { 属性显示ID:7, 属性status:NOCHANGE, 属性代码:TS, 属性描述:独立淋浴和浴缸 } ], roomTypeDisplayId:34, 房间类型ID:24, roomTypeStatus:无更改, roomPool:HOSP, 最大职业状态:无变化, 新入住率:4, 当前最大入住率:4, 电容状态:无变化, 新增产能:1, 当前容量:1, 保证:1,, 可利用率:1, 溢价:是的, 精英可用性:错误, 伊斯罗:是的 } ]
} 您可以尝试以下方法:

常数数据={ versionRoomPoolList:[{ pmsRoomType:[城市], versionRoomTypeAttributeList:[{ attributeDisplayId:2, 属性status:NOCHANGE, 属性代码:CV, 属性说明:城市视图 }, { 属性显示ID:6, 属性status:NOCHANGE, 属性代码:HF, 属性描述:高层、8层及以上 }, { 属性显示ID:4, 属性status:NOCHANGE, 属性代码:KN, 属性描述:1张大床 }, { 属性显示ID:5, 属性status:NOCHANGE, 属性代码:NE, 属性说明:电梯附近 }, { 属性显示ID:7, 属性status:NOCHANGE, 属性代码:RF, 属性说明:移动锁 }, { attributeDisplayId:3, 属性status:NOCHANGE, 属性代码:SB, 属性说明:沙发床 }, { attributeDisplayId:1, 属性status:NOCHANGE, 属性代码:苏, 属性描述:阿尔卑斯酒店客房 }, { 属性显示ID:8, 属性status:NOCHANGE, 属性代码:TS, 属性描述:独立淋浴和浴缸 } ], roomTypeDisplayId:31, 房间类型ID:21, roomTypeStatus:无更改, 房间池:城市, 最大职业状态:无变化, 新入住率:4, 当前最大入住率:4, 电容状态:无变化, 新增容量:12, 当前容量:12, 保证:9, 米纳瓦 可行性:1, 溢价:错, 精英可用性:错误, 伊斯罗:错 }, { versionRoomTypeList:[{ pmsRoomType:[HOSP], versionRoomTypeAttributeList:[{ attributeDisplayId:2, 属性status:NOCHANGE, 属性代码:CV, 属性说明:城市视图 }, { 属性显示ID:5, 属性status:NOCHANGE, 属性代码:HF, 属性描述:高层、8层及以上 }, { attributeDisplayId:1, 属性status:NOCHANGE, 属性代码:HS, 属性描述:酒店套房 }, { 属性显示ID:4, 属性status:NOCHANGE, 属性代码:KN, 属性描述:1张大床 }, { 属性显示ID:6, 属性status:NOCHANGE, 属性代码:RF, 属性说明:移动锁 }, { attributeDisplayId:3, 属性status:NOCHANGE, 属性代码:SB, 属性说明:沙发床 }, { 属性显示ID:7, 属性status:NOCHANGE, 属性代码:TS, 属性描述:独立淋浴和浴缸 } ], roomTypeDisplayId:34, 房间类型ID:24, roomTypeStatus:无更改, roomPool:HOSP, 最大职业状态:无变化, 新入住率:4, 当前最大入住率:4, 电容状态:无变化, 新增产能:1, 当前容量:1, 保证:1,, 可利用率:1, 溢价:是的, 精英可用性:错误, 伊斯罗:是的 }, { pmsRoomType:[HOSP], versionRoomTypeAttributeList:[{ attributeDisplayId:2, 属性status:NOCHANGE, 属性代码:CV, 属性说明:城市视图 }, { 属性显示ID:6, 属性status:NOCHANGE, 属性代码:HF, 属性描述:高层、8层及以上 }, { attributeDisplayId:1, 属性status:NOCHANGE, 属性代码:HS, 属性描述:酒店套房 }, { 属性显示ID:4, 属性status:NOCHANGE, 属性代码:KN, 属性描述:1张大床 }, { 属性显示ID:5, 属性status:NOCHANGE, 属性代码:NE, 属性说明:电梯附近 }, { 属性显示ID:7, 属性status:NOCHANGE, 属性代码:RF, 属性说明:移动锁 }, { attributeDisplayId:3, 属性status:NOCHANGE, 属性代码:SB, 属性说明:沙发床 }, { 属性显示ID:8, 属性status:NOCHANGE, 属性代码:TS, 属性描述:独立淋浴和浴缸 } ], roomTypeDisplayId:35, 房间类型ID:25, roomTypeStatus:无更改, roomPool:HOSP, 最大职业状态:无变化, 新入住率:4, 当前最大入住率:4, 电容状态:无变化, 新增产能:1, 当前容量:1, 保证:1,, 可利用率:1, 溢价:是的, 精英可用性:错误, 伊斯罗:错 } ], roomPoolDisplayId:4, RoomPool状态:无更改, 房间代码:HOSP, Miosrc:YD, sna:N, 最大职业状态:无变化, 新入住率:4, 当前最大入住率:4, 电容状态:无变化, 新增产能:2, 当前容量:2, 溢价:是的, 精英可用性:错误, 总是:假, 排他性:假, 仅属性:false }, { pmsRoomType:[HOSP], versionRoomTypeAttributeList:[{ attributeDisplayId:2, 属性status:NOCHANGE, 属性代码:CV, 属性说明:城市视图 }, { 属性显示ID:5, 属性status:NOCHANGE, 属性代码:HF, 属性描述:高层、8层及以上 }, { 属性显示 Id:1, 属性status:NOCHANGE, 属性代码:HS, 属性描述:酒店套房 }, { 属性显示ID:4, 属性status:NOCHANGE, 属性代码:KN, 属性描述:1张大床 }, { 属性显示ID:6, 属性status:NOCHANGE, 属性代码:RF, 属性说明:移动锁 }, { attributeDisplayId:3, 属性status:NOCHANGE, 属性代码:SB, 属性说明:沙发床 }, { 属性显示ID:7, 属性status:NOCHANGE, 属性代码:TS, 属性描述:独立淋浴和浴缸 } ], roomTypeDisplayId:34, 房间类型ID:24, roomTypeStatus:无更改, roomPool:HOSP, 最大职业状态:无变化, 新入住率:4, 当前最大入住率:4, 电容状态:无变化, 新增产能:1, 当前容量:1, 保证:1,, 可利用率:1, 溢价:是的, 精英可用性:错误, 伊斯罗:是的 } ] } 常量myFunction=data=>{ 数据 versionRoomPoolList:data.versionRoomPoolList.filteri=>{ 删除i.pmsRoomType; 返回i; }, };
logmyFunctiondata您的数据不是数组,而是具有versionRoomPoolList字段的对象

map函数按顺序为数组中的每个元素调用一次提供的回调函数,并根据结果构造一个新数组

我认为这就是您要寻找的:创建新的对象数据,将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)

您的代码基本上是正确的,正如Felix Kling在您的帖子中所评论的,您只需要保护不存在的属性。这可以简单到:

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

也就是说。。。我怀疑您只是输入了一个错误,您应该将versionRoomTypeList更改为versionRoomTypeAttributeList:-P

数组中的每个对象似乎都有versionRoomTypeList属性,因此出现了错误。我无法使用rawData,因为我是从端点获取此数据的。因此,您可以调整方法。将您的响应设置为一个名为rawData的变量,或者您可以自己将我的逻辑转换为一个函数。我已经用一个代码片段更新了我的代码,您可以在其中进行测试。该示例将从每个对象中删除所有pmsRoomType键。不幸的是,我无法使用此键,因为我无法使用babel之类的工具来传输我的代码。这一特定功能必须支持内部业务应用程序的IE7和IE8。