Free Porn
xbporn

https://www.bangspankxxx.com

2 Simple Ways to Remove a Key from an Object in JavaScript

You often need to remove some keys from an object in Javascript. Here are the two simple ways to delete a key from an object in JavaScript.

Removing a key from an object with the delete operator

You can use the delete operator to remove a key from an object in JavaScript. You just have to mention the key with the delete operator. Check the example below.

The output of the above code will be:

“John”

undefined

You can also use the other syntax with the delete operator. See the example given below.

The above code will do the same thing and the output will be the same as above.

Using undefined to remove a key from an object

You can also set the key of an object to undefined which will also remove the key from that object. Check the example.

output:

“John” 

undefined

Using the other syntax for removing the key from the object.

The output will be the same as above. This second method also deletes a key from an object.

How to delete a key from a nested object in JavaScript?

You can use the delete operator or set the value to undefined to a nested object too. Here is an example.

The above code will generate the following output.

97134567891

undefined

You can look into the delete operator in detail in mozila developer guide.

Conclusion

You can use the delete operator to remove a key from an object or use the other method which sets the key to undefined. Both these methods remove a key from an object in JavaScript.

Here is a way to remove a key from an object without mutating the original object.

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here