[英]Who to map through a nested object and delete a key value pair in 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
函数依次对数组中的每个元素调用提供的回调函数,然后从结果中构造一个新数组。
我认为这就是您要寻找的东西:使用versionRoomPoolList
作为键和一个新数组作为值来创建新对象( data
)。
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)
您可以尝试如下操作:
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))
您的代码大部分是正确的,并且正如Felix Kling在您的帖子中评论的那样,您只需要防止出现不存在的属性即可。 这可以很简单:
if (roompool.versionRoomTypeList && roompool.versionRoomTypeList.length) {
// delete
}
就是说...我怀疑您只是输入错误,应该将versionRoomTypeList
更改为versionRoomTypeAttributeList
:-P
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.