Tuesday, 29 August 2017

How do I remove a key from a JavaScript object?




Let's say we have an object with this format:



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



I wanted to do a function that removes by key:



removeFromObjectByKey('Cow');

Answer



The delete operator allows you to remove a property from an object.



The following examples all do the same thing.




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

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

// Example 3
delete thisIsObject.Cow;



If you're interested, read Understanding Delete for an in-depth explanation.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...