Deleting Properties
The only way to actually remove a property from an object is to use thedelete
operator; setting the property to undefined
or null
only remove the value associated with the property, but not the key.
var obj = {
bar: 1,
foo: 2,
baz: 3
};
obj.bar = undefined;
obj.foo = null;
delete obj.baz;
for(var i in obj) {
if (obj.hasOwnProperty(i)) {
console.log(i, '' + obj[i]);
}
}
The above outputs both bar undefined
and foo null
- only baz
was removed and is therefore missing from the output.