javascript - 谁在 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
方法,但我被困在如何做到这一点上,还是那是正确的方法?
解决方案
你的数据不是一个数组,它是一个带有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)
推荐阅读
- symfony - Webpack Encore select2 not working with not a function import error
- c++ - c ++将两个字节解析为int
- c# - 如何将单个密钥文件链接到解决方案中的每个项目?
- php - 使用php搜索包含两个针的多维数组
- slack - Slack - 复制频道名称
- java - 这个 CRON 表达式的间隔是多少?
- python-3.x - 在窗口中使用许多按钮时的 Pygame 代码减少
- java - 在不受 Spring 管理的类中使用 KafkaListener
- c# - 使用 WCF 客户端的并行请求
- sql-server - 分配唯一随机案例编号 SQL 2008