Javascript Array removeItem(value)

Description

Javascript Array removeItem(value)


Array.prototype.removeItem = function removeItem(value) {
    arr = arr.filter(function (elem)  {
       return value !== elem;
    });//w  w w . j  a v a2s . c o m
    console.log(arr);
}

var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arr.removeItem(1);

var arr = ['hi', 'bye', 'hello' ];
arr.removeItem('bye');

Javascript Array removeItem(value)

Array.prototype.removeItem = function(value) {
    var result = [];
    for (i in this) {
        if (this[i] === value) {
            this.splice(i, 1);/*from   w  w  w  .  j a  v a  2 s  .  c  o  m*/
        }
    }
    console.log(this);
}

var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arr.removeItem(1);
var arr = ['hi', 'bye', 'hello' ];
arr.removeItem('bye');

Javascript Array removeItem(value)

Array.prototype.removeItem = function removeItem(value) {
    arr = arr.filter(function (elem)  {
       return value !== elem;
    });/* w  w  w .ja va 2 s.co m*/
    console.log(arr);
};

var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arr.removeItem(1);

var arr = ['hi', 'bye', 'hello' ];
arr.removeItem('bye');

Javascript Array removeItem(value)

Array.prototype.removeItem = function(value) {
    while (this.indexOf(value) >= 0) {
        this.splice(this.indexOf(value), 1);
    }/*from  ww w  .j ava  2 s .  com*/
    console.log(this);
    return this;
};
var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arr.removeItem(1);

var arr1 = ['hi', 'bye', 'hello' ];
arr1.removeItem('bye');

Javascript Array removeItem(value)

Array.prototype.removeItem = function(value) {
    //from w  w  w  .j  av a2  s . c  o m
    while (this.indexOf(value) >= 0) {
        this.splice(this.indexOf(value), 1);
    }
    console.log(this);
       return this;
};

var arrOne = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arrOne.removeItem(1);

var arrTwo = ['hi', 'bye', 'hello' ];
arrTwo.removeItem('bye');

Javascript Array removeItem(value)

Array.prototype.removeItem = function (value) {
    for (i = 0; i < this.length; i++) {
        if (this[i] === value) {
            this.splice(i, 1);/*w  ww .  j  a va 2s  .  c o  m*/
        }
    }
}

var arrOne = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arrOne.removeItem(1);
console.log(arrOne);

var arrTwo = ['hi', 'bye', 'hello'];
arrTwo.removeItem('bye');
console.log(arrTwo);

Javascript Array removeItem(value)

Array.prototype.removeItem = function(value) {
    return this.filter(function(el) {
        return el !== value;
    });/*w w  w  .j  av a  2  s  . c  o m*/
}

var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
console.log(arr.removeItem(1));

var arr1 = ['hi', 'bye', 'hello' ];
console.log(arr1.removeItem('bye'));

Javascript Array removeItem(value)

"use strict";/*ww  w. j  a v a  2s. c  o  m*/

var arr = ['1','2','3','4','5'];
var modifiedArr = arr.removeItem('2');

Array.prototype.removeItem = function removeItem(value) {
    for (var i in this) {
        var item = this[i];

        if (value === item) {
            this.splice(i, 1);
            return this;
        }
    }
}

Javascript Array removeItem(value)

Array.prototype.removeItem = function(value) {
    for (i = 0; i < this.length; i++) {
        if(this[i] === value) {
            this.splice(i, 1);//  w  ww . j a  va  2s .  c  o m
        }
    }
}

var arr1 = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arr1.removeItem(1);
console.log(arr1);

var arr2 = ['hi', 'bye', 'hello' ];
arr2.removeItem('bye');
console.log(arr2);

Javascript Array removeItem(value)

Array.prototype.removeItem=function(value){
    for(var index in this ){
        if(this[index]==value){
            this.splice(index,1);/*w  w w  .jav  a  2  s  .  c  o  m*/
        }
    }
    console.log(this);
}
var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arr.removeItem(1);


var arr = ['hi', 'bye', 'hello' ];
arr.removeItem('bye');

Javascript Array removeItem(value)

Array.prototype.removeItem = function (value) {
    for (var i = 0; i < this.length; i++) {
        var currElem = this[i];

        if (currElem === value) {
            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'];
console.log(arr.removeItem(1));

var arr = ['hi', 'bye', 'hello'];
console.log(arr.removeItem('bye'));

Javascript Array removeItem(value)

Array.prototype.removeItem = function(value){
    for (var i = 0; this.indexOf(value) >= 0; i++){
        var a = this.indexOf(value);
        this.splice(this.indexOf(value), 1);

    }//from   w  w w . j a v a2  s. co m
    console.log(this);
}

var arrFi = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arrFi.removeItem(1);

var arrSe = ['hi', 'bye', 'hello'];
arrSe.removeItem('bye');

Javascript Array removeItem(value)

Array.prototype.removeItem =  function (value){
 for(var i = this.length - 1; i > 0; i--){
  if(this[i] === value)
  {/*w  w w .ja va2  s .com*/
   this.pop(i);
  }
 }
};

function  removeTheItem(arr, value){
 for(var i = arr.length - 1; i >0 ; i--){
  if(arr[i]===value){

  }
 }
}

Javascript Array removeItem(value)

Array.prototype.removeItem = function removeItem(value) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === value) {
            this.splice(i, 1);/*from  w ww.ja  va  2 s  .  co m*/
            i--;
        }
    }
    return console.log(this);
}
var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arr.removeItem(1);
arr = ['hi', 'bye', 'hello' ];
arr.removeItem('bye');

Javascript Array removeItem(value)

Array.prototype.removeItem = function (value) {
    var items = this.slice(0),
        newArray = [],/*ww w. ja v a  2 s.com*/
        i;

    for(i = 0; i < items.length; i++) {
        if(items[i] === value) {
            continue;
        }
        newArray.push(items[i]);
    }

    return newArray;
};

var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
var newArr = arr.removeItem(1);
console.log(newArr);

var arr = ['hi', 'bye', 'hello' ];
var newArr = arr.removeItem('bye');
console.log(newArr);

Javascript Array removeItem(value)

Array.prototype.removeItem = function removeItem(value) {
    var filteredArray = this;
    for (var i = 0; i < filteredArray.length; i++) {
        if (value === this[i]) {
            filteredArray.splice(i, 1);/*from www. j  a  v  a2s.c o  m*/
        }
    }
    return filteredArray;
}

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'));

Javascript Array removeItem(value)

"use strict";/*ww w. j a  v a  2 s  .  c  o m*/
Array.prototype.removeItem = function (value) {
    for (var i in this) {
        if(this[i] === value) {
            this.splice(i, 1);
        }
    }
};

//examples below

var arr = ['hi', 'bye', 'hello' ];
console.log('before: ' + JSON.stringify(arr));
arr.removeItem('bye');
console.log('after: ' + JSON.stringify(arr));

var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
console.log('before: ' + JSON.stringify(arr));
arr.removeItem(1);
console.log('after: ' + JSON.stringify(arr));

Javascript Array removeItem(value)

/**//ww  w.j a va2s.c  o  m
 * Created by PC on 18.01.2016 ?..
 */
"use script"
Array.prototype.removeItem = function(value) {
    if(typeof value === 'number' || typeof value === 'string') {
        for (var index in this) {
            if(this[index] === value) {
                this.splice(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);

Javascript Array removeItem(value)

Array.prototype.removeItem = function(value){
    var removePos = [];
    console.log(this);/*from   ww w .  j  a  v  a  2 s .c o m*/
    for(var i in this){
        if(this[i] === value) {
            removePos.unshift(i);
        }
    }

    for(var i = 0; i < removePos.length; i++) {
        this.splice(parseInt(removePos[i]), 1);
    }
    console.log(this);
}

var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arr.removeItem(1);

var arr = ['hi', 'bye', 'hello' ];
arr.removeItem('bye');

Javascript Array removeItem(value)

Array.prototype.removeItem = function (value) {
    var len = this.length;
    var resultArr = [];
    for (var i = 0; i < len; i++) {
        if (this[i] !== value) {
            resultArr.push(this[i]);/*from  ww w  .  ja  va 2s.co m*/
        }
    }
    return resultArr;
};

var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
console.log(arr.removeItem(1));
arr = ['hi', 'bye', 'hello' ];
console.log(arr.removeItem('bye'));

Javascript Array removeItem(value)

'use strict'/*from  w  w  w  .  ja v a 2s  . c o  m*/
Array.prototype.removeItem = function (value){
    this.forEach(function (elem, index, arr) {
        if (elem === value) {
            arr.splice(index, 1);
        }
    });

    console.log(this);
}

var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arr.removeItem(1);
//output: [2, 4, 3, 4, 111, 3, 2, '1']

var arr = ['hi', 'bye', 'hello'];
arr.removeItem('bye');
//output: ['hi', 'hello']

Javascript Array removeItem(value)

Array.prototype.removeItem = function(value) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == value && (typeof arr[i] == typeof value)){
            var index = arr.indexOf(arr[i]);
            if (index > -1) {
                arr.splice(index, 1);/*ww  w. j a v a2  s.co  m*/
                i--;
            }
        }
    }
    console.log(arr);
};
var arr = ['hi', 'bye', 'hello' ];
arr.removeItem('bye');

Javascript Array removeItem(value)

Array.prototype.removeItem = function(value) {
    for (var i = 0; i < this.length; i++) {
        if(this[i] === value) {
            this.splice(i, 1); //At position i, remove 1 item
        }/*from  w w w  .j  a v  a2 s . c om*/
    }
};
var arrFirst = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
arrFirst.removeItem(1);
console.log(arrFirst);

var arrSecond = ['hi', 'bye', 'hello' ];
arrSecond.removeItem('bye');
console.log(arrSecond);

Javascript Array removeItem(value)

/**// www .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.
 */
Array.prototype.removeItem = function (value) {
    for (var i = 0; i < this.length; i++) {
        var currElem = this[i];

        if (currElem === value) {
            this.splice(i, 1);
        }
    }

    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'));

Javascript Array removeItem(value)

// Write a JavaScript function removeItem(value) that accept as parameter a number or string.
// The function should remove all elements with the given value from an array. Attach the
// function to the Array type. You may need to read about prototypes in JavaScript and
// how to attach methods to object types. You should return as a result the modified array.

Array.prototype.removeItem = function (value) {
    return this.filter(function (v) {
        return v !== value;
    })//from  w  w w  . j  av a2s.  c  o  m
};

var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
console.log(arr.removeItem(1));

arr = ['hi', 'bye', 'hello' ];
console.log(arr.removeItem('bye'));

Javascript Array removeItem(value)

/*/*  w w w . j  a va  2  s  .  c  o m*/
    Write a JavaScript function removeItem(value) that
    accept as parameter a number or string.
    The function should remove all elements with the given value from an array.
    Attach the function to the Array type.
    You may need to read about prototypes in
    JavaScript and how to attach methods to object types.
    You should return as a result the modified array.
*/

Array.prototype.removeItem = function (value) {
    return this.filter(function (val) {
        return val !== value;
    });
}

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'));

Javascript Array removeItem(value)

/**//  w w w  .ja  va  2s.co  m
 * Write a JavaScript function removeItem(value) that accept as parameter a number or string.
 * The function should remove all elements with the given value from an array.
 * Attach the function to the Array type.
 * You may need to read about prototypes in JavaScript and how to attach methods to object types.
 * You should return as a result the modified array.
 */




Array.prototype.removeItem = function (value) {
    for(var i = this.length-1; i--;){
        if (this[i] === value) {
            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 arr1 = ['hi', 'bye', 'hello' ];
arr1.removeItem('bye');
console.log(arr1);

Javascript Array removeItem(value)

/*Problem 5. * Array Prototype Function
 Write a JavaScript function removeItem(value) that accept as parameter a number or string.
  The function should remove all elements with the given value from an array. Attach the function to the Array type.
   You may need to read about prototypes in JavaScript and how to attach methods to object types.
   You should return as a result the modified array. Write a sample program to demonstrate that your
   function works correctly for the examples below:*/

 var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];

Array.prototype.removeItem = function (value) {
    for (var i = 0; i < arr.length; i++) {
        if(arr[i] === value) {
            delete arr[i];/*  ww w.  j a v  a 2 s  .  c om*/
        }
    }

    var resultArr = [];
    var indexResultArr = 0;

    for (var j = 0; j < arr.length; j++) {
        if(arr[j] !== undefined) {
            resultArr[indexResultArr] = arr[j];
            indexResultArr++;
        }


    };

    console.log(resultArr);
};


arr.removeItem(1);



PreviousNext

Related