21

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 ?

Rachel
  • 100,387
  • 116
  • 269
  • 365
  • possible duplicate of [How to remove a property from a javascript object](http://stackoverflow.com/questions/208105/how-to-remove-a-property-from-a-javascript-object) – Thomas May 28 '12 at 19:42

2 Answers2

34

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];
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • http://stackoverflow.com/questions/6485127/how-to-delete-unset-the-properties-of-a-javascript-object just incase u want second thought on delete – Mrigesh Raj Shrestha Aug 03 '12 at 12:12
  • @MrigeshRajShrestha: Except that the accepted answer there is wrong. – T.J. Crowder Aug 03 '12 at 16:07
  • i pulled it easily with splice; dunno what happened there.. u got jsfiddle for the issue?? – Mrigesh Raj Shrestha Aug 24 '12 at 12:02
  • 1
    @MrigeshRajShrestha: `splice` is for dealing with properties classed as *array indexes* (see [§15.4 of the spec](http://ecma-international.org/ecma-262/5.1/#sec-15.4) for which properties are classed that way). I don't know what you mean by "u got jsfiddle for the issue?". If you mean the error I pointed out in the accepted answer there, look again at my comment; there's a link to a jsbin post (jsbin is like jsfiddle) showing how it's wrong. – T.J. Crowder Aug 24 '12 at 12:43
  • @TJ Crowder jsfiddle.net is a web app that allows online storage and execution of your script. good in case you want your scripts debugged by others without sharing whole info.. check that site; its awesome – Mrigesh Raj Shrestha Sep 18 '12 at 05:02
  • @MrigeshRajShrestha: I'm familiar with jsfiddle.net (and jsbin.com, and codepen.io). That wasn't the part of the sentence that was unclear to me. Re-read the comment. – T.J. Crowder Sep 18 '12 at 07:11
6

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];
Russ Cam
  • 124,184
  • 33
  • 204
  • 266