Nodejs Array Remove remove(elementValue)

Here you can find the source of remove(elementValue)

Method Source Code

// Problem 2. Remove elements
////from w ww  .ja va2 s.co 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.


Array.prototype.remove = function (elementValue) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === elementValue) {
            this.splice(i, 1);
            i--;
        }
    }
}

Array.prototype.toString = function () {
    var result = '[' + this.join(', ') + ']' + ' -> type of elements: [';
    for (var i = 0; i < this.length; i++) {
        if (i === this.length-1) {
            result += typeof (this[i]) + ']';
        }
        else {
            result += typeof (this[i]) + ', ';
        }
    }
    return result;
}

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

console.log('Original array: \n' + arr.toString());
arr.remove(1);
console.log('\nAfter removing elements with value 1:\n' + arr.toString());

Related

  1. remove(element)
    Array.prototype.remove = function(element){
      var i = 0;
      var len = this.length;
      for(i = len - 1; i >= 0; i--){
        if(this[i] === element){
          this.splice(i,1);
    
  2. remove(element)
    Array.prototype.remove = function (element) { 
        var myArray = new Array;
        this.forEach(function(item) {
           if(item !== element)
               myArray.push(item);
        });
        this.splice(0);
        for(var i = 0; i < myArray.length; i++)
            this.push(myArray[i]);
    ...
    
  3. remove(element)
    var arrayElements = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
    Array.prototype.remove = function (element) {
        var elements = [];
        for (var i = 0; i < this.length; i++) {
            if (this[i] !== element) {
                elements.push(this[i]);
        return elements;
    ...
    
  4. remove(element, howMany)
    Array.prototype.remove = function(element, howMany) {
      var idx
      if (-1 !== (idx = this.indexOf(element))) {
        this.splice(idx, howMany || 1)
    
  5. remove(elementVal)
    Array.prototype.remove = function(elementVal){
        var len = this.length;
       for(var i =0;i<len;i+=1)
            if(this[i]===elementVal)
                this.splice(i,1);
                i-=1;
    var arr = [1,1,1,2,2,2,2,1,1,1,1,1,1];
    arr.remove(1);
    console.log(arr);
    
  6. remove(f)
    Array.prototype.remove  = function(f){
      var ME=this;
      if(typeof(f)=="function"){
        ME.each(function(s,i){
          if(f(s,i))ME.splice(i,1);
        },-1);
      return ME;
    };
    ...
    
  7. remove(filter)
    Array.prototype.remove = function(filter)
      return this.select(function(row){
        return !filter(row);
      });
    
  8. remove(fn, v)
    Array.prototype.remove = function(fn, v) { 
        var self = this;
        var isFN = typeof(fn) === 'function';
        var isV = v !== undefined;
        var arr = [];
        for (var i = 0, l = self.length; i < l; i++) {
            if (isFN) {
                if (!fn.call(self, self[i], i)) {
                    arr.push(self[i]);
    ...
    
  9. remByVal(val)
    Array.prototype.remByVal = function (val) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] === val) {
                this.splice(i, 1);
                i--;
        return this;
    };
    ...