Javascript delete object property not working
Had a similar problem. This worked for me:
// create a new copy
let newUser= ({...user}._doc);
// delete the copy and use newUser that thereafter.
delete newUser.password;
Just do:
user.password = undefined;
instead of:
delete user.password;
and the password property will not appear at the output.
there are certain rules for delete operator in javascript
- if the property is an own non-configurable property in "strict mode" than it will return false.
for example
x = 42; // creates the property x on the global object
var y = 43; // creates the property y on the global object, and marks it as non-configurable
// x is a property of the global object and can be deleted
delete x; // returns true
// y is not configurable, so it cannot be deleted
delete y; // returns false
- If the object inherits a property from a prototype, and doesn't have the property itself, the property can't be deleted by referencing the object. You can, however, delete it directly on the prototype.
for example
function Foo(){}
Foo.prototype.bar = 42;
var foo = new Foo();
// returns true, but with no effect,
// since bar is an inherited property
delete foo.bar;
// logs 42, property still inherited
console.log(foo.bar);
so, please cross check these point and for more information your can read this Link