Javascript Array remove(from, to)
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); };
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); }; [1,2,3,4].remove(2);/*from w w w.j ava2 s . c o m*/
/**/* w ww . j av a2 s. co m*/ * A much nicer way to remove from the built-in array * @param from {number} The first element to remove. * If the "to" parameter is blank, this is the only * element that will be removed. * @param to {number} The last element to remove. */ 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); };
function print(str) { console.log(str);//ww w . j a v a 2 s . co m } Array.prototype.remove = function(from, to) { var rest = this.slice( (to || from) + 1 || this.length); console.log("rest:"+ rest); this.length = from < 0 ? this.length + from : from; console.log("this:" + this); return this.push.apply(this, rest); } var array = ["one", "two","three","four","five","six"]; print(array); array.remove(1,2); print(array);
Array.prototype.remove = function(from, to) { var rest = this.slice((to || from) + 1 || this.length); this.length = from < 0 ? this.length + from : from; this.push.apply(this, rest)//w w w .j a v a 2 s. c o m return this; }; function permutate(n, a) { if (a.length === 0) { all.push(n); } else { for(var i = 0; i < a.length; i++) { var n_new = n.slice(0); n_new.push(a[i]); permutate(n_new, a.slice(0).remove(i,i)); } } } var all = []; permutate([], [1, 2, 3, 4]); for(var i = 0; i < all.length; i++) { console.log(all[i]); }