How do I 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 regex to end up with new myObject as follows?

var myObject = 
{

  ircEvent: PRIVMSG,
  method: newURI 
};
 
Text Copied From stackoverflow.com
on : Thu, Oct 16, 2008 Views : 1549463

Accepted Answer

Like this:

delete myObject.regex;
 
// or, delete myObject['regex'];
 
// or, var prop = regex;
 delete myObject[prop];
 

Demo

var myObject = 
{

  ircEvent: PRIVMSG,
  method: newURI,
  regex: ^http:
//.* 
};
 delete myObject.regex;
  console.log(myObject);

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.

Text Copied From stackoverflow.com
on : Thu, Oct 16, 2008

Ask Question

If you want to ask question you must LogIn or SignUp
Login SignUp

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp