0
根据docs,删除操作符应该能够从对象中删除属性。我正在尝试删除“falsey”对象的属性。如何删除对象属性?
例如,我认为下面将删除所有来自testObj的falsey性质的,但它并不:
var test = {
Normal: "some string", // Not falsey, so should not be deleted
False: false,
Zero: 0,
EmptyString: "",
Null : null,
Undef: undefined,
NAN: NaN // Is NaN considered to be falsey?
};
function isFalsey(param) {
if (param == false ||
param == 0 ||
param == "" ||
param == null ||
param == NaN ||
param == undefined) {
return true;
}
else {
return false;
}
}
// Attempt to delete all falsey properties
for (var prop in test) {
if (isFalsey(test[prop])) {
delete test.prop;
}
}
console.log(test);
// Console output:
{ Normal: 'some string',
False: false,
Zero: 0,
EmptyString: '',
Null: null,
Undef: undefined,
NAN: NaN
}