Im having trouble unsetting a key within an array using prototype (though this is probably a general javascript question) — I have the following bit of code within my custom prototype class:
initialize: function(selector) {
this.selected = [];
this.selectcount = 0;
},
addSelected: function(value) {
this.selected.push(value);
this.selectcount++;
},
removeSelected: function(value) {
delete this.selected[value];
this.selectcount--;
},
adding to the array works fine, but I cant for the life of me figure out how to delete the specific ID within the array (value is referencing a particular id it is adding to the selected array, and I need to remove that same value from the array when removeSelected is clicked..) — i also tried
this.selected.splice(value, 1);
But that didnt work either. Im basically looking or the equivelent of unset() in php.
Any help on what Im missing would be greatly appreciated.
Thanks in advance!
You are on the right track with
splice(), but you need to pass it in the index of the value in the array, instead of the value itself.indexOf()is the method to use for this:indexOf()returns -1 if the value does not exist in the array.Note that
Array.indexOf()is not supported on some older browsers, but you are safe to use it with Prototype.js, as it adds it for you.