Organizational Research By

Surprising Reserch Topic

How to remove a property from a JavaScript object


 I create an object thusly:

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

What is the best way to remove the property 'regex' to end up with this new myJSONObject:

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

asked May 19, 2015 in JAVASCRIPT by rajesh
0 votes
16 views



Related Hot Questions

2 Answers

0 votes
delete myJSONObject.regex;
// or,
delete myJSONObject['regex'];
// or,
var prop = "regex";
delete myJSONObject[prop];
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 his blog, Understanding delete. It is highly recommended.
answered May 19, 2015 by rajesh
0 votes
var myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};

delete myJSONObject.regex;

alert ( myJSONObject.regex); // alerts: undefined

This works in Firefox and Internet Explorer, and I think it works in all others.

answered May 19, 2015 by rajesh

...