Javascript Array remove(value)
// Remove value from Array. Array.prototype.remove = (value) => { const position = this.indexOf(value) if (~position) this.splice(position, 1) return this/*from www . j a va2 s . c om*/ }
Array.prototype.remove = function(value) { for (var i = 0; i < this.length; i++) { if (this[i] === value) { this.splice(i, 1);//w w w . j a va 2 s . c o m } } }; var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; arr.remove(1); console.log(arr);
Array.prototype.remove = function(value) { while (this.indexOf(value) >= 0) { this.splice(this.indexOf(value), 1); }//from www. ja v a 2 s . c om return this; }; var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; console.log('Initial array: ' + arr); arr.remove(1); console.log('After remove: ' + arr);
/* Write a function that removes all elements with a given value. Attach it to the array type. */ Array.prototype.remove = function (value) { return this.filter(function(el) { return el !== value; });/*from w w w . j av a2 s . co m*/ }; var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; console.log(arr.remove(1)); //arr = [2,4,3,4,111,3,2,'1'];
Array.prototype.remove = function(value){ var i,/*from w w w .j a va 2 s. co m*/ l; for(i=0,l=this.length;i<=l;i+=1){ if(this[i]===value){ this.splice(i,1); } } }; var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; arr.remove(1); console.log(arr);
// Remove element from array Array.prototype.remove = function(value) { var idx = this.indexOf(value); if (idx != -1) { return this.splice(idx, 1); }/*from ww w . j ava 2 s . com*/ return false; };
?//Problem 2. Remove elements //Write a function that removes all elements with a given value. //Attach it to the array type. //Read about prototype and how to attach methods. Array.prototype.remove = function (value) { var i,/* w ww. j av a2 s . c o m*/ len = this.length; for (i = 0; i < len; i+=1) { if (this[i] === value) { this.splice(i, 1); i -= 1; } } } var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; arr.remove(1); console.log(arr);
var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; console.log('Task 2:'); console.log('Old array: '); console.log(arr);/*from w ww. ja va 2 s. c o m*/ Array.prototype.remove = function (value) { while (this.indexOf(value) >= 0) { this.splice(this.indexOf(value), 1); } return this; }; arr.remove(1); console.log('Array after removing number 1:'); console.log(arr); console.log('----------------------------------------------------');
Array.prototype.remove = function(value){ for (var i = 0; i < this.length; i++){ if (this[i] === value){ this[i] = this[this.length - 1] this.pop()// w w w . j a v a2 s. c o m i-- } } }
'use strict'/*from w w w . j a v a 2 s. c o m*/ Array.prototype.removeItem = function remove(value) { var i; var arrNew = []; for (i = 0; i < arr.length; i++) { if (arr[i] !== value) { arrNew.push(arr[i]); } } return arrNew; } 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.remove = function (value) { if (this == null) { throw new TypeError();// w w w .j a v a2 s . c o m } var result = []; var size = this.length; for (var i = 0; i < size; i++) { //for (var i in this) ?? ?? ????? ???? ? ?? ?? ???? if (this[i] != value) { result.push(this[i]); } } return result; };
console.log('\nProblem 2. Remove elements'); /*Write a function that removes all elements with a given value. Attach it to the array type.// w ww. ja va 2s . c om Read about prototype and how to attach methods.*/ Array.prototype.remove = function(value) { while (this.indexOf(value) >= 0) { this.splice(this.indexOf(value), 1); } return this; }; function test(){ var array = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; console.log('Before removing: ', array); array.remove(1); console.log('After removing', array); } test();
Array.prototype.remove = function(value){ return(this.filter(function(n){ return n != value; })); };
// Problem 2. Remove elements // Write a function that removes all elements with a given value. // Attach it to the array type. // Read about prototype and how to attach methods. // var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; // arr.remove(1); //arr = [2,4,3,4,111,3,2,'1']; Array.prototype.remove = function remove(value) { var i,//from www. j a v a2 s . c o m len; for (i = 0, len = this.length; i < len; i+=1) { if (this[i] === value) { this.splice(i, 1); } } }; (function main() { var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; arr.remove(1); console.log(arr); })();
//Problem 2. Remove elements ///*from w w w.j a v a2s . c o m*/ //Write a function that removes all elements with a given value. // Attach it to the array type. // Read about prototype and how to attach methods. var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; Array.prototype.remove = function(value){ for (var i = 0; i < this.length; i++){ if (value === this[i]){ this.splice(i, 1); } } return this; } Array.prototype.removeSecondWay = function(value){ return this.filter(function(index){ return this[index] !== value; } ) } console.log(arr.join(', ')); console.log(arr.remove(1).join(', ')); console.log(arr.removeSecondWay(1).join(', '));
// Removes an element from an array. // String value: the value to search and remove. // return: an array with the removed element; false otherwise. Array.prototype.remove = function (value) { var index = this.indexOf(value) if (index != -1) { return this.splice(index, 1) // The second parameter is the number of elements to remove. }/*w w w . jav a2s. c o m*/ return false }
// Removes an element from an array. // String value: the value to search and remove. // return: an array with the removed element; false otherwise. Array.prototype.remove = function(value) { var index = this.indexOf(value); if (index != -1) { return this.splice(index, 1); // The second parameter is the number of elements to remove. }/*w w w . j a va 2 s. co m*/ return false; }
Array.prototype.remove = function(value) { var arr = this; var index = arr.indexOf(value); if (index === -1) return false; var len = arr.length; if (!len) return false; while (index < len) { arr[index] = arr[index+1];/* w ww. ja v a 2 s. c o m*/ index++ } arr.length--; return true; };
/**/*from w ww. j av a2 s . co m*/ * Problem 2. Remove elements Write a function that removes all elements with a given value. Attach it to the array type. Read about prototype and how to attach methods. var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; arr.remove(1); //arr = [2,4,3,4,111,3,2,'1']; */ Array.prototype.remove = function(value) { while (this.indexOf(value) >= 0) { this.splice(this.indexOf(value), 1); } return this; }; var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1'], element = 1, result, i, len; result = 'Array after removing: '; if (!isNaN(element)) { result += arr.remove(element * 1); } else { result += arr.remove(element); } console.log('Problem 2: ' + result);
//Problem 2. Remove elements //// w w w . ja v a2s.c o m //Write a function that removes all elements with a given value. // Attach it to the array type. // // Read about prototype and how to attach methods. // // var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; // arr.remove(1); //arr = [2,4,3,4,111,3,2,'1']; // var arr, arrOriginal; Array.prototype.remove = function (value) { var i; for (i = 0; i < this.length; i += 1) { if (this[i] === value) { this.splice(i, 1); i--; } } return this; } arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; arrOriginal = arr.slice(); console.log(arrOriginal + ' remove 1 --> ' + arr.remove(1));
Array.prototype.remove = function(value) { for(var i = 0; i<this.length; i++) if(this[i] == value) this.splice(i,1); }
/*Problem 2.//from w w w .j a va2 s . c om Write a function that removes all elements with a given value. Attach it to the array type. Read about prototype and how to attach methods.*/ Array.prototype.remove = function (value) { for (var i = 0, len = this.length; i < len; i += 1) { if (this[i] === value) { this.splice(i, 1); } } return this; }; var arr = [1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 11, '1']; var valueToRemove = 1; console.log('Original array: ' + arr); console.log('Output (removing \'' + valueToRemove + '\'): ' + arr.remove(valueToRemove)); arr = ['one', 'two', 'three', 'ten']; valueToRemove = 'ten'; console.log('Original array: ' + arr); console.log('Output (removing \'' + valueToRemove + '\'): ' + arr.remove(valueToRemove));