Nodejs Array Remove by Value removeElementremoveElement(element)

Here you can find the source of removeElementremoveElement(element)

Method Source Code

//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.removeElement = function removeElement(element){
    for (var i = 0; i < arr.length; i+=1) {
        if(arr[i]===element){
            arr.splice(i,1)//from w ww .ja  v a  2  s . com
        }
    }
    return arr;
};

console.log(arr.removeElement(1));

Related

  1. removeByValue(value)
    Array.prototype.removeByValue = function(value) {
        for (var i = 0; i < this.length; ++i) {
            if (this[i] == value) {
                this.splice(i, 1);
    
  2. removeByValue(value)
    Array.prototype.removeByValue = function (value) {
        var i = 0,
            count = 0;
        for (i = 0; i < this.length; i += 1) {
            if (this[i] === value) {
                this.splice(i, 1);
                i -= 1;
                count += 1;
        return count;
    };
    var fruits = ["Apple", "Banana", "Orange", "Apple", "Mango", "Apple"];
    var arr = [1, 3, 1, 1, 4, 5, 8, 1];
    var value;
    var removedElelemnts = 0;
    jsConsole.writeLine('array: [' + arr + ']');
    value = 1;
    jsConsole.writeLine('remove elements with value: ' + value);
    removedElelemnts = arr.removeByValue(value);
    jsConsole.writeLine('number of removed elements: ' + removedElelemnts);
    jsConsole.writeLine('array: [' + arr + ']');
    jsConsole.writeLine('');
    jsConsole.writeLine('fruits array: [' + fruits + ']');
    value = 'Apple';
    jsConsole.writeLine('remove elements with value: ' + value);
    removedElelemnts = fruits.removeByValue(value);
    jsConsole.writeLine('number of removed elements: ' + removedElelemnts);
    jsConsole.writeLine('fruits array: [' + fruits + ']');
    
  3. removeElement(value)
    Array.prototype.removeElement = function(value){
      if(this.indexOf(value) != -1) {
        this.splice(this.indexOf(value), 1)  
    
  4. removeElement(valueElement)
    Array.prototype.removeElement = function(valueElement){
        for(var i = this.length - 1; i >= 0; i-=1) {
            if(this[i] === valueElement) {
                this.splice(i, 1);
    };
    var arr=[1,1,1,1,1,3,5,6,7,8,9];
    arr.removeElement(1);
    ...
    
  5. removeElement(element)
    Array.prototype.removeElement = function(element) {
      var idx = this.indexOf(element);
      if (idx <= (-1)) {
        return;
      return this.remove(idx);
    };
    
  6. removeElements(elements)
    Array.prototype.removeElements = function (elements) {
      elements.forEach((el) => {
        this.splice(this.indexOf(el), 1)
      })
    
  7. removeItem(value)
    Array.prototype.removeItem = function (value) {
        for (var i in this) {
            if(this[i] === value){
                this.splice(i, 1);
        return this;
    };
    var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
    ...
    
  8. removeItem(value)
    Array.prototype.removeItem = function (value) {
        for (i = 0; i < this.length; i++) {
            if (this[i] === value) {
                this.splice(i, 1);
    var arrOne = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
    arrOne.removeItem(1);
    ...
    
  9. removeItem(value)
    Array.prototype.removeItem = function (value) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] === value) {
                this.splice(i, 1);
        return this;
    var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
    ...