/ JAVASCRIPT, FRONT-END

How to remove a key from an object in Javascript?

Question

I have an object below:

var thisIsObject= {
   'Cow' : 'Moo',
   'Cat' : 'Meow',
   'Dog' : 'Bark'
};

I want to remove the key Dog and its value. What should I do?

Answer

There are three ways to remove a key from an object.

// Example 1
var key = "Dog";
delete thisIsObject[key]; 

// Example 2
delete thisIsObject["Dog"];

// Example 3
delete thisIsObject.Dog;

Reference


This is the end of post