How do I remove a property from a JavaScript object

0 votes

 I create an object as follows:

let 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?

let myObject = {
    "ircEvent": "PRIVMSG",
    "method": "newURI"
};
Jun 8, 2020 in Java-Script by kartik
• 37,520 points
1,546 views

1 answer to this question.

0 votes

Objects in JavaScript can be thought of as maps between keys and values.

 The delete operator is used to remove these keys, more commonly known as object properties, one at a time.

var obj = {
  myProperty: 1    
}
console.log(obj.hasOwnProperty('myProperty')) // true
delete obj.myProperty
console.log(obj.hasOwnProperty('myProperty')) // false

The delete operator does not directly free memory, and it differs from simply assigning the value of null or undefined to a property, in that the property itself is removed from the object.

 Note:  If the value of a deleted property was a reference type (an object), and another part of your program still holds a reference to that object, then that object will, of course, not be garbage collected until all references to it have disappeared.

Hope it helps!

answered Jun 8, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
1 answer

How do I delete a specific element from an array in JavaScript?

You can use the splice() method. Code: let arr ...READ MORE

answered 4 days ago in Java-Script by Navya
23 views
0 votes
1 answer

How do I pass variables and data from PHP to JavaScript?

Hello @kartik, Simply use one of the following ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,840 points
1,930 views
0 votes
1 answer

How do I include a JavaScript file in another JavaScript file?

Hello @kartik, It is possible to dynamically generate ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,840 points
892 views
0 votes
1 answer

How do I send a cross-domain POST request via JavaScript?

Hello @kartik, Follow this steps: Create an iFrame, put a ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,840 points
1,884 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,193 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,051 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
3,009 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
45,094 views
0 votes
1 answer

How do I remove a property from a JavaScript object?

Hello @kartik, The delete operator is used to remove properties ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,840 points
942 views
0 votes
1 answer

How do I search for a key of object in javascript?

Use hasOwnProperty(key) for (let i = 0; i ...READ MORE

answered Oct 14, 2020 in Java-Script by Niroj
• 82,840 points
2,568 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP