Where I write code.

Friday, 23 August 2019

Remove property from Javascript Object


How to remove property from a JavaScript object

If you want to remove property from a JavaScript object then you can do by following ways.


Remove JavaScript Object Property

lets create one object so we can run and test it.

var user = {
    "id": "1001",
    "name": "Vikas",
    "email": "test@test.com",
    "password": "TesTP2ss",
    "createdAt": "20-08-2019"
};
The  will be as follows:

remove property from JS object

Now we can remove the object property from the following ways:

Using delete operator:

as we have seen the user's object now before sending to frontend we will delete the password property and then we will send it.


delete user.password;
>>true

Or alternatively, you can do the following way as well.

delete user['password'];
>>true


remove property from JS object


If delete won't work then you can use the following:
user.password = undefined;

Please let me know if it won't work.


No comments:

Post a comment