-
弑天下
您的数据不是数组,而是具有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)
-
鸿蒙传说
您可以尝试如下操作: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