Javascript Array remove(element) method
Array.prototype.remove = function(element) { var index = this.indexOf(element); if (index != -1) this.splice(index, 1);//from w ww. jav a 2 s. co m }
Array.prototype.remove = function(element){ var i = 0;/*from w w w . j av a 2 s . c o m*/ var len = this.length; for(i = len - 1; i >= 0; i--){ if(this[i] === element){ this.splice(i,1); } } }
Array.prototype.remove = function(element){ for(var i = 0; i < this.length; i++){ if(this[i] === element){ this.splice(i,1);/* w w w . ja va 2 s .com*/ } } }
/* /*from w w w . j ava 2 s . c om*/ Write a function that makes a deep copy of an object. The function should work for both primitive and reference types. */ Array.prototype.remove = function(element){ var i=0; for(i;i<this.length;i+=1){ if(element===this[i]){ this.splice(i, 1); i-=1; } } }; var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; arr.remove(2); console.log(arr.join(' '));
Array.prototype.remove = function (element) { var pos = this.indexOf(element); while (pos !== -1) { this.splice(pos,1);//ww w .jav a2 s . c om pos = this.indexOf(element); } }; var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; console.log(arr); arr.remove(1); console.log(arr);
'use strict';/*from w w w . j av a 2 s . c om*/ var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; arr.remove(1); Array.prototype.remove = function(element){ var arr = this, removed = []; for (var i = 0; i < arr.length; i += 1) { if (arr[i] !== element) { removed.push(arr[i]); } } return removed; };
// Problem 2. Remove elements // Write a function that removes all elements with a given value. // Attach it to the array type. Array.prototype.remove = function(element) { return this.filter(function(item) { return item !== element; });/* ww w .j av a 2 s . c om*/ }; var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'], result = arr.remove(1); console.log(arr); console.log(result);
Array.prototype.remove = function (element) { var i;/* w ww .j a va2s . c om*/ for (i = 0; i < this.length; i += 1) { if (this[i] === element) { this.splice(i, 1); } } }; var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; console.log(arr); arr.remove(1); console.log(arr); arr.remove(3); console.log(arr);
// 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(element) { for (let i = this.length; i >= 0; i--) { if (this[i] === element) { this.splice(i, 1);//ww w . j a v a 2s. co m } } }; let arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; console.log(arr); arr.remove(1) console.log(arr);
// Problem 2. Remove elements // Write a function that removes all elements with a given value. // Attach it to the array type. Array.prototype.remove = function(element) { for (var index = 0; index < this.length; index += 1) { if (this[index] === element) { this.splice(index, 1);/*from ww w. j a v a 2 s . c o m*/ } } }; var array = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; console.log(array); array.remove(1); console.log(array);
// Problem 2. Remove elements // Write a function that removes all elements with a given value. Attach it to the array type. Array.prototype.remove = function (element) { var clearedArray = []; for (var i = 0; i < this.length; i += 1) { if (this[i] !== element) { clearedArray.push(this[i]);/*from www . j a va2s . c o m*/ } } return clearedArray; }; var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; console.log(arr.remove(1)); // Expected: arr = [2,4,3,4,111,3,2,'1'];
function solve(args) { Array.prototype.remove = function(element) { for (var i = 0; i < this.length; i++) { /*ww w . j av a2 s. com*/ if(this[i] == element){ this.splice(i, 1); i--; } } } args.remove(args[0]); for (var i = 0; i < args.length; i++) { console.log(args[i]); } } solve( [ 1,2,1,4,1,3,4,1,111,3,2,1,'1' ] );
?/*Write a function that removes all elements with a given value. Attach it to the array type.// w ww . j a v a2s . co m 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 (element) { var len = this.length; for (var i = 0; i < len; i++) { if (this[i] === element) { this.splice(i, 1); i -= 1; } } return this; } arr.remove(1); console.log(arr.join(', '));
Array.prototype.remove = function(element) { for (var i = 0; i < this.length; i++) { if (this[i] == element) { this.splice(i,1); } }/* w w w . jav a 2s . c o m*/ };
Array.prototype.remove = function(element) { var i = this.indexOf(element); if(i != -1) {/*w w w . ja v a 2s .c om*/ this.splice(i, 1); } }
// 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 (element) { for (var i = 0; i < this.length; i+=1) { if (this[i] === element) { this.splice(i, 1);// w w w .j ava 2 s .c o m i-=1; } } }; var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; console.log('Initial array -- > ' + arr.join()); arr.remove(1); console.log('Removed num 1 -- > ' + arr.join());
//Write a function that removes all elements with a given value. //Attach it to the array type. Array.prototype.remove = function (element) { var result = [], index,/* w w w . ja v a2s. c om*/ len; for(index = 0, len = this.length; index < len; index += 1) { if (this[index] !== element) { result.push(this[index]); } } return result; }; var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; console.log(arr.remove(1));
// 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(element) { var i=0;// w ww . j a v a 2s. co m for(i;i<this.length;i+=1) { if(this[i]===element) { this.splice(i, 1); --i; } } }; var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; console.log(arr); arr.remove(1); console.log(arr);
var arr = [];//ww w.j a v a 2 s . c om Array.prototype.remove = function(element){ for(var el in arr){ if(arr[el] === element){ arr.splice(el,1); } } return arr; } function removeElements(){ arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; var target = parseInt(document.getElementById('input-task2').value); arr = arr.remove(target); document.getElementById('result-task2').textContent = arr.join(', '); }
function removeElements(){ var inputArray = document.getElementById("inputArray").value.split(","); var removeELement = document.getElementById("elementToRemove").value; inputArray.remove(removeELement);/*w ww . j a v a 2 s.co m*/ document.getElementById("result").innerHTML = inputArray.join(","); } Array.prototype.remove = function(element){ var newArray = new Array(); for(var current in this){ if(this[current] == element){ this.splice(current, 1); } } }
Array.prototype.remove = function(element) { var i, len = this.length; for(i = 0; i < len; i += 1){ if(this[i] === element){ this.splice(i ,1);// w w w . ja va2s .c o m i -= 1; } } return this; } var arr = [2, 3, 1, 4, 5, 1]; console.log('The array before remove: ' + arr.join(',')); arr.remove(1); console.log('The array after remove the number 1: ' + arr.join(','));
var inputArray = prompt("Enter elements: ", '[1, 2, 2, 3, 3, 3, 4, 4, 4, 4]'); var removeElement = prompt("Remove element: ", '3'); var elements = []; elements = inputArray.replace(/[\[\]' ]+/g, '').split(','); Array.prototype.remove = function (element) { for (var i = this.length; i--;) { if (this[i] === element) { this.splice(i, 1);//w w w . jav a2s. c om } } return this; }; elements.remove(removeElement); document.writeln("New array is : ",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(element) { var arr = this; var final = []; for (var i = 0; i < arr.length; i++) { if(arr[i] !== element) { final.push(arr[i]);//from w ww. jav a2 s .c om } } return final; } var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; var newww = arr.remove(1); //arr = [2,4,3,4,111,3,2,'1']; console.log(newww);
//### 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. ///*w w w. j a va2s . co m*/ // 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(element){ for(var ind = 0; ind < this.length; ind++){ if(this[ind] === element){ this.splice(ind, 1); --ind; } } return this; } var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; console.log(arr.join(', ')); arr.remove(1); console.log(arr.join(', '));
/**// w w w . java 2 s .c o 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(element) { var len; for(ind=0,len=this.length;ind<len;ind+=1) { if(this[ind]===element) { this.splice(ind,1); ind-=1; } } return this; } var arr=[1,1,1,2,3,4,1,2,1]; console.log(arr.remove(1));
/* Write a function that removes all elements with a given value/*from ww w .j a va 2s . c o m*/ ? Attach it to the array type ? Read about prototype and how to attach methods */ Array.prototype.remove = function(element){ for (var i = 0; i < this.length; i++) { if(this[i] == element){ this.splice(i, 1); i--; } }; } var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; var arr2= [2,2,2,2,3,3,2,2,1,1,1,3,2,321,2,3,4,2,2,2,2,1,1,2,3,2,2,1,1,1,2,3,4,2,5] console.log('The full array is ' + arr); arr.remove(1); console.log('The array without selected elements is ' + arr) arr2.remove(2); console.log(arr2)
/**/*from ww w.j a v a 2 s .com*/ 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 (element) { var i, fixedArr = []; for (i in this) { if (this[i] !== element) { fixedArr.push(this[i]); } } return fixedArr; } var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; var changedArr = arr.remove(1); var len = changedArr.length; for (var i = 0; i < len -1; i += 1) { console.log(changedArr[i]); }
//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 = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; Array.prototype.remove = function (element) { for (var i = 0; i < this.length; i+=1) { if (this[i] === element) { this.splice(i, 1);//www.j a va 2 s.c o m i-=1; } } return this; }; console.log(arr.remove(1));
Array.prototype.remove = function remove(element) { var arr = this, length = arr.length, currentElemnent = element;//from w w w .j a va 2 s . co m if (currentElemnent[0] === '\'' || currentElemnent[0] === '"') { currentElemnent = currentElemnent .substr(1, currentElemnent.length - 2) .toString(); } else { currentElemnent = currentElemnent | 0; } for (var index = 0; index < length; index += 1) { if (arr[index] === currentElemnent) { arr[index] = undefined; } } };
//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 (element) { for (var ind = 0; ind < this.length; ind++) { if (this[ind] === element) { this.splice(ind, 1);/*from w w w . j a va2 s. com*/ --ind; } } return this; } var arr = [1, 2, 3, 4, 1, 2, 3, 1, 5, 1, 2,]; jsConsole.writeLine(arr.join(', ')); arr.remove(1); jsConsole.writeLine(arr.join(', '));
/* Problem 2: Write a function that removes all elements with a given value. Attach it to the array type./* w ww . j a va 2 s . c o m*/ Read about prototype and how to attach methods. */ Array.prototype.remove = function (element) { var fixedArr = []; for (var i in this) { if (this[i] !== element) { fixedArr.push(this[i]); } } return fixedArr; } var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; var changedArr = arr.remove(1); for (var i = 0; i < changedArr.length -1; i+=1) { console.log(changedArr[i]); }
// 2. Write a function that removes all elements with a given value. var array = [1, 1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; Array.prototype.remove = function (element) { if (this.length < 1) { console.log("The array object is empty."); }/*from w ww . ja v a 2 s . c o m*/ var found = false; for (var i = 0; i < this.length; i++) { if (this[i] === element) { this.splice(i, 1); i--; found = true; } }; if (!found) { console.log("The element is not found."); } }; console.log(array); array.remove(1); console.log(array);
/*/*w w w .j a v a 2s.c o 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']; */ console.log('Problem 2. Remove elements'); Array.prototype.remove= function(element){ while(this.indexOf(element)>=0){ this.splice(this.indexOf(element),1); } return this; } var arr=[1,2,1,4,1,3,4,1,111,3,2,1,'1']; console.log('array:') console.log(arr); arr.remove(1); console.log('array after remove:') console.log(arr); console.log('\n')
/*// w w w. j a v a2 s. c o 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']; */ console.log('Problem 2. Remove elements'); Array.prototype.remove = function (element) { while(this.indexOf(element)>=0){ this.splice(this.indexOf(element),1); } return this; } var arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; console.log(arr); arr.remove(1); //arr = [2,4,3,4,111,3,2,'1']; console.log(arr); console.log('*****************************************');
/**/*from ww w. j a v a 2 s .c o m*/ * Removes the given element from this array. */ Array.prototype.remove = function(element) { var res = false; for (var i = 0; i < this.length; i++) { if (this[i] == element) { this.splice(i--, 1); res = true; } } return res; };
/**/*from w ww.j av 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 = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'], elementToRemove = 1; Array.prototype.remove = function (element) { var index = this.indexOf(element); while (index > -1) { this.splice(index, 1); index = this.indexOf(element); } }; console.log("REMOVE ELEMENTS"); console.log("Array:", arr); arr.remove(elementToRemove); console.log("Array after remove:", arr);
//Write a function that removes all elements with a given value // 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']; //Attach it to the array type //Read about prototype and how to attach methods Array.prototype.remove = function (element) { var newArr = []; var count = 0; for (var i = 0; i < this.length; i++) { if(this[i] === element) {//from ww w .ja va 2 s .com continue; } else { newArr[count] = this[i]; } count++; } return newArr; } var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; var arrayAfterRemovedElement = arr.remove(1); console.log(arrayAfterRemovedElement);
/// <reference path="c:\users\m.tonkov\documents\visual studio 2013\Projects\JS_UsingObjects_HW\JS_UsingObjects_HW\js-console.js" /> // previous row enables the use of 'jsConsole' Array.prototype.remove = function (element) { for (var i = 0, len = this.length; i < len; i++) { if (this[i] === element) { this.splice(i, 1);/*from ww w .j a v a 2 s . c om*/ } } return this; } var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1']; function removeElement() { var input = document.getElementById("input").value, isInputAString = document.getElementById("checkbox").checked; if (!isInputAString) { input = parseInt(input); } arr.remove(input); jsConsole.writeLine(arr); }
Array.prototype.remove = function (element) { var index = this.indexOf(element); this.splice(index, 1);/* w ww . jav a 2 s . co m*/ };
//Write a function that removes all elements with a given value. //Attach it to the array type. //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 input = [1,2,1,4,1,3,4,1,111,3,2,1,'1']; removeElementsTest();/* w w w.j a va 2s. c om*/ function removeElementsTest() { console.log('Initial array: ' + input); input.remove(1); console.log('Removing 1 : ' + input); input.remove(2); console.log('\nRemoving 2 : ' + input); } Array.prototype.remove = function (element) { var arr = this, index; for (index in arr) { if (arr[index] === element) { arr.splice(index, 1); } } };
/* Problem 2. Remove elements Write a function that removes all elements with a given value. Attach it to the array type./* ww w. j a v a 2 s. c om*/ 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 array = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'], // You can edit these variables element = 1; Array.prototype.remove = function (element) { var i, length; for (i = 0, length = this.length; i < length; i += 1) { if (this[i] === element) { this.splice(i, 1); i -= 1; } } return this; }; console.log('Initial array is: ' + array); array.remove(element); console.log('Removing ' + element + ' results in: ' + array);
Array.prototype.remove = function(element) { var index = this.indexOf(element); if(index != -1) { this.splice(index, 1);//from w ww . j a v a 2 s. c om } }