我需要一些帮助来按键过滤对象属性,但保留白名单中的过滤键为空。
我正在使用SO上的这个答案中的函数:
这是第一个对象的过滤函数输出,对我很有用,因为它包含白名单中所有值:
问题在于我需要筛选出白名单中存在的键,它们不能被删除。如果在原始对象中不存在,则希望它们为空。
为了澄清,我需要第二个对象看起来像这样:
如何调整过滤函数以实现此功能?
我正在使用SO上的这个答案中的函数:
https://dev59.com/21kT5IYBdhLWcg3wV-CY#38750895
预览:
const raw = {
item1: { key: 'sdfd', value:'sdfd' },
item2: { key: 'sdfd', value:'sdfd' },
item3: { key: 'sdfd', value:'sdfd' }
};
const allowed = ['item1', 'item3'];
const filtered = Object.keys(raw)
.filter(key => allowed.includes(key))
.reduce((obj, key) => {
obj[key] = raw[key];
return obj;
}, {});
console.log(filtered);
在我的方案中,我将其作为白名单:
var whitelist = ["ApartmentNumber", "FloorSpace", "PrimaryRoomSpace", "CaseStatusType", "AskingPrice"];
这是原始对象:
{
"Id": 668,
"ProjectId": 667,
"NumberOfBedrooms": 2,
"FloorSpace": 60,
"PrimaryRoomSpace": 60,
"LandLotType": "None",
"ApartmentNumber": "L-101\r",
"PublicationTitle": "L-101",
"PublicationInformationId": 49,
"CaseStatusId": 227,
"CaseStatusType": "Ready",
"CaseStatusName": "Salgsklar",
"CaseReferralKey": "80-17-10001",
"AskingPrice": 10000000,
"EnergyLabel": "None",
"HeatingGrade": "None",
"Comission": 0,
"DocumentCostBasis": 10000000,
"PricePerSquareMeter": 166666.66666666666666666667,
"Street": "Street 1",
"ZipCode": "0682",
"BuyerIds": [],
"ResponsibleBrokerId": 0,
"BrokerId": 0
},
{
"Id": 669,
"ProjectId": 667,
"NumberOfBedrooms": 2,
"NumberOfBedroomsMax": 2,
"FloorSpace": 0,
"FloorSpaceMax": 100,
"PrimaryRoomSpace": 90,
"PrimaryRoomSpaceMax": 90,
"ApartmentNumber": "L-107\r",
"CaseStatusId": 207,
"CaseStatusType": "Active",
"CaseStatusName": "Oppdrag akseptert",
"CaseReferralKey": "80-17-10007",
"StoreyOf": 1,
"EnergyLabel": "None",
"HeatingGrade": "None",
"Comission": 0,
"DocumentCostBasis": 0,
"Street": "Street 2",
"ZipCode": "0682",
"BuyerIds": [],
"ResponsibleBrokerId": 0,
"BrokerId": 0
},
{
"Id": 670,
"ProjectId": 667,
"NumberOfBedrooms": 2,
"NumberOfBedroomsMax": 2,
"FloorSpace": 0,
"FloorSpaceMax": 100,
"PrimaryRoomSpace": 90,
"PrimaryRoomSpaceMax": 90,
"ApartmentNumber": "L-106\r",
"CaseStatusId": 223,
"CaseStatusType": "General",
"CaseStatusName": "Bestilt info",
"CaseReferralKey": "80-17-10006",
"StoreyOf": 1,
"EnergyLabel": "None",
"HeatingGrade": "None",
"Comission": 0,
"DocumentCostBasis": 0,
"Street": "Street 1",
"ZipCode": "0682",
"BuyerIds": [],
"ResponsibleBrokerId": 0,
"BrokerId": 0
},
{
"Id": 671,
"ProjectId": 667,
"NumberOfBedroomsMax": 1,
"FloorSpace": 0,
"FloorSpaceMax": 50,
"ApartmentNumber": "L-102\r",
"CaseStatusId": 219,
"CaseStatusType": "General",
"CaseStatusName": "All info mottatt",
"CaseReferralKey": "80-17-10002",
"StoreyOf": 2,
"EnergyLabel": "None",
"HeatingGrade": "None",
"Comission": 0,
"DocumentCostBasis": 0,
"PricePerSquareMeter": 0,
"Street": "Street 1",
"ZipCode": "0682",
"BuyerIds": [],
"ResponsibleBrokerId": 0,
"BrokerId": 0
}
这是第一个对象的过滤函数输出,对我很有用,因为它包含白名单中所有值:
{ ApartmentNumber: 'L-101\r',
FloorSpace: 60,
PrimaryRoomSpace: 60,
CaseStatusType: 'Ready',
AskingPrice: 10000000 }
但对于第二个对象,它会看起来像这样。由于第二个对象中没有AskingPrice,因此它缺失了:
{ ApartmentNumber: 'L-107\r',
FloorSpace: 0,
PrimaryRoomSpace: 90,
CaseStatusType: 'Active' }
问题在于我需要筛选出白名单中存在的键,它们不能被删除。如果在原始对象中不存在,则希望它们为空。
为了澄清,我需要第二个对象看起来像这样:
{ ApartmentNumber: 'L-107\r',
FloorSpace: 0,
PrimaryRoomSpace: 90,
CaseStatusType: 'Active',
AskingPrice: ''} //<-- empty string
如何调整过滤函数以实现此功能?
typeof raw[key] === 'undefined'
。使用raw[key] === undefined
比较raw[key]
和变量undefined
,但是undefined
是不存在的。如果在之前执行const undefined = 50;
,那么它就不再起作用了。此外,filtered
是一个const
。 - Orelsanplsraw[key] === 'undefined'
,如果你使用raw[key]
作为条件,它没有定义时条件就不会被满足。 - cнŝdkraw[key] = false
或raw[key] = 0
,你就会有一些问题。 - Orelsanpls