How do I remove a property from a JavaScript object

0 votes

Say 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"
};
Jul 27, 2020 in Java-Script by kartik
• 37,510 points
694 views

1 answer to this question.

0 votes

Hello @kartik,

The delete operator is used to remove properties from objects.

const obj = { foo: "bar" }
delete obj.foo
obj.hasOwnProperty("foo") // false

Note that, for arrays, this is not the same as removing an element. To remove an element from an array, use Array#splice or Array#pop. For example:

arr // [0, 1, 2, 3, 4]
arr.splice(3,1); // 3
arr // [0, 1, 2, 4]

Hope it helps!!

Thank you!!

answered Jul 27, 2020 by Niroj
• 82,880 points

Related Questions In Java-Script

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,880 points
1,535 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,880 points
550 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,880 points
1,303 views
0 votes
1 answer

How do I test for an empty JavaScript object?

Hello @kartik, Try this: var obj = {}; return Object.keys(obj).length; Hope ...READ MORE

answered Aug 28, 2020 in Java-Script by Niroj
• 82,880 points
376 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,880 points
21,754 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,880 points
2,647 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,880 points
2,505 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,880 points
41,378 views
0 votes
1 answer

How do I remove a property from a JavaScript object?

Objects in JavaScript can be thought of ...READ MORE

answered Jun 8, 2020 in Java-Script by Niroj
• 82,880 points
1,100 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,880 points
2,071 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