Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a property from an object?

Tags:

Currently an event is set on checkboxes, and event.target gives me the status (checked = true/false) of checkbox which is clicked.

I am maintaining an object which keeps the track on all the selected checkboxes

var selectedMap  = {};  if(event.target == true){     var key = event.target.id;     var val = event.target.name;     selectedMap[key] = val; } 

and I want to remove the element from the map which is unselected

else if(event.target == false){   selectedMap.remove(event.target.id); } 

when I run this it gives me error in Firebug : selectedMap.remove is not a function

So my question is How can I remove the element when the checkbox is unselected ?

like image 965
Rachel Avatar asked Nov 14 '10 15:11

Rachel


People also ask

How do I remove a property from an object?

Remove Property from an ObjectThe delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.

How do you remove a property?

Click Admin, and navigate to the property you want to delete. In the PROPERTY column, click Property Settings. Click Move to Trash Can. On the confirmation screen, click Move property to Trash Can.

How do I remove a property from an object in C#?

You can't remove a property, unless you remove it permanently, for all cases. What you can do, however, is to create multiple classes, in a class hierarchy, where one class has the property and the other hasn't.


2 Answers

Using delete:

delete selectedMap[event.target.id]; 

You're setting the value incorrectly, though. Here's the correct way:

if(event.target == true){     var key = event.target.id;   // <== No quotes     var val = event.target.name; // <== Here either     selectedMap[key] = val; } 

In fact, you could:

if(event.target == true){     selectedMap[event.target.id] = event.target.name; } 

Getting the event target stuff out of the way, it's easier to envision this with simple strings:

var obj = {}; obj.foo = "value of foo"; alert(obj.foo);    // alerts "value of foo" without the quotes alert(obj["foo"]); // ALSO alerts "value of foo" without the quotes, dotted notation with a literal and bracketed notation with a string are equivalent delete obj.foo;    // Deletes the `foo` property from the object entirely delete obj["foo"]; // Also deletes the `foo` property from the object entirely var x = "foo"; delete obj[x];     // ALSO deeltes the `foo` property 

When using a plain object like this, I always use a prefix on my keys to avoid issues. (For instance, what would happen if your target element's ID was "toString"? The object already has an [inherited] property called "toString" and things would get Very Weird Very Quickly.)

So for me, I do this:

if(event.target == true){     selectedMap["prefix" + event.target.id] = event.target.name; } 

...and of course:

delete selectedMap["prefix" + event.target.id]; 
like image 137
T.J. Crowder Avatar answered Oct 07 '22 01:10

T.J. Crowder


What you have is an object and not an array (although an array is an object). You declare an object literal with {} whereas an array literal is declared with [].

You can use delete to remove an object property like so

delete selectedMap[event.target.id]; 
like image 20
Russ Cam Avatar answered Oct 07 '22 00:10

Russ Cam