I found this function on the john resig blog for removing an element from an array. It works really well! but I don't really understand how..
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
I'm confused about what is happening with this statement: (to || from) + 1 || this.length) for starters; perhaps once I understand that, the rest will become more clear. Any help sussing out exactly what's happening here is much appreciated. Thanks.
.spliceinstead?Array#splice(start, length)does pretty much the same thing. You can also insert elements at the same time. That's what it's for. The only caveat is that it doesn't support negative values forlength.