gpt4 book ai didi

javascript - 谁在 JavaScript 中通过嵌套对象进行映射并删除键值对

转载 作者:行者123 更新时间:2023-12-02 23:43:36 24 4
gpt4 key购买 nike

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

{
"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)

关于javascript - 谁在 JavaScript 中通过嵌套对象进行映射并删除键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55956262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com