johnstok johnstok - 1 year ago 139
Javascript Question

How to remove a property from a JavaScript object?

Say I create an object as follows:

var myObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};

What is the best way to remove the property
to end up with new
as follows?

var myObject = {"ircEvent": "PRIVMSG", "method": "newURI"};

Answer Source

Like this:

delete myObject.keyname;
// or,
delete myObject["keyname"];

For anyone interested in reading more about it, Stack Overflow user kangax has written an incredibly in-depth blog post about the delete statement on their blog, Understanding delete. It is highly recommended.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download