Javascript Array removeItem()
method
Array.prototype.removeItem = function() { for (var item in arguments) { // this way I can remove items with different values at once. Try it with arr.removeItem(1, 4); for (var i = 0; i < this.length; i++) { if (this[i] === arguments[item]) { this.splice(i, 1);// w w w .ja v a 2 s . c o m } } } } var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; arr.removeItem(1); console.log(arr); arr = ['hi', 'bye', 'hello']; arr.removeItem('bye'); console.log(arr);
'use strict'//from w ww .j av a 2 s .c o m Array.prototype.removeItem = function(item){ for(var i = 0; i < this.length; i ++){ if(item === this[i]){ this.splice(i, 1); } } } var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; arr.removeItem(1); console.log(arr); var secArr = ['hi', 'bye', 'hello' ]; secArr.removeItem('hi'); console.log(secArr);
Array.prototype.removeItem = function(item) { for (var i = 0; i < this.length; i++) { if (this[i] === item) { this.splice(i, 1);/*from www . ja va2s . com*/ } } } var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; arr.removeItem(1); console.log(arr); var arr = ['hi', 'bye', 'hello' ]; arr.removeItem('bye'); console.log(arr);
Array.prototype.removeItem = function removeItem(item){ do {/*w ww.j a va 2 s. c o m*/ var index = this.indexOf(item); if(item === this[index]) this.splice(index,1); } while(index > -1) }; var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; arr.removeItem(1); console.log(arr); arr = ['hi', 'bye', 'hello' ]; arr.removeItem('bye'); console.log(arr);
Array.prototype.removeItem = function(item){ while(this.indexOf(item) !== -1) { this.splice(this.indexOf(item), 1); }/* w w w . j ava2s . com*/ return this; } var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; console.log(arr.removeItem(1)); var arr = ['hi', 'bye', 'hello' ]; console.log(arr.removeItem('bye'));
Array.prototype.removeItem = function (item) { var result = []; for (var i = 0; i < this.length; i++) { if (this[i] !== item) { result.push(this[i]);// w w w.j ava2s. c o m } } return result; }; var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; arr = arr.removeItem(1); console.log(arr); var arr = ['hi', 'bye', 'hello']; arr = arr.removeItem('bye'); console.log(arr);
Array.prototype.removeItem = function(item) { var index = this.indexOf(item); if(index > -1){ this.splice(index,1);//from www . j a va 2s.c o m } return this; }; var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; arr = arr.removeItem(1); console.log('[' + arr.join(', ') + ']'); var arr = ['hi', 'bye', 'hello' ]; arr.removeItem('bye') console.log('[' + arr.join(', ') + ']');
Array.prototype.removeItem = function removeItem(item) { "use strict"; do {//from w w w . j av a2 s. c o m var index = this.indexOf(item); if (item === this[index]) { this.splice(index, 1); } } while (index > -1); }; var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; arr.removeItem(1); console.log(arr); arr = ['hi', 'bye', 'hello' ]; arr.removeItem('bye'); console.log(arr);
Array.prototype.removeItem = function(item) { for ( var i = 0 ; i < this.length ; i++ ) { if ( this[i] == item ) { this.remove(i);/*from ww w . ja v a2s. co m*/ break; } } };