Javascript Array remove()

Description

Javascript Array remove()


Array.prototype.remove = function() {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];//from  w  ww  .  jav a2 s.c o  m
        while ((ax = this.indexOf(what)) !== -1) {
            this.splice(ax, 1);
        }
    }
    return this;
};

Javascript Array remove()

Array.prototype.remove = function() {
    const toRemove = this.shift();
    let newArr = [];

    for(const i of this)
        if(i !== toRemove)
            newArr.push(i);// w ww. ja v a  2s .c o  m

    this.splice(this);

    for(const i of newArr)
        this.push(i);
}

function solve(arr) {
    arr.remove();
    arr.forEach(x => console.log(x));
}

solve([ '1', '2', '3', '2', '1', '2', '3', '2' ]);

Javascript Array remove()

Array.prototype.remove = function() {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];//w w  w  . j  a  v a2s . c  o m
        while ((ax = this.indexOf(what)) !== -1) {
            this.splice(ax, 1);
        }
    }
    return this;
};

Array.prototype.contains = function(value) {
    return (this.indexOf(value) > -1);
}

Javascript Array remove()

Array.prototype.remove = function () {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];/*from  w  w  w  . j a  va  2 s . co m*/
        while ((ax = this.indexOf(what)) !== -1) {
            this.splice(ax, 1);
        }
    }
    return this;
};

var ar = [1, 2, 1, 3, 1, 4, 1];
ar.remove(1);

for (var i = 0; i < ar.length; i++) {
    jsConsole.writeLine(ar[i]);
}

Javascript Array remove()

/*//w  ww . ja  va  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 toRemove = 1;
Array.prototype.remove = function () {
    for (var i in this) {
        if (this[i] === toRemove) {
            this.splice(i, 1);
        }
    }
}

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

Javascript Array remove()

Array.prototype.remove = function(){
    var args = [].slice.call(arguments);
    var removed = [];
    var self = this;
    var len = args.length;
    for(var i = 0; i < len; i++){
        var index;
        while((index = this.indexOf(args[i])) >= 0){
            this.splice(index,1);/*from  w  ww  .j  av  a 2 s .c  o m*/
            removed.push(args[i]);
        }
    }
    return removed;
}
var a = [1,2,3,4,5,6,7,5,8];
console.log(a.remove(3,4,5,10));
console.log(a);

Javascript Array remove()

Array.prototype.remove = function () {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === arguments[0]) {
            this.splice(i, 1);//from   w w  w  .  j  a  v  a  2s.c om
            i--;
        }
    }
}

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

function removeElements() {
    console.log(arr);
    arr.remove(1);
    document.getElementById("result").value = arr;
    console.log(arr);
}

Javascript Array remove()

?//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.
//arr.remove(1); //arr = [2,4,3,4,111,3,2,'1']

Array.prototype.remove = function () {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === arguments[0]) {
            this.splice(i, 1);/*ww w. ja  v  a2s .  co  m*/
            i--;
        }
    }
}

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

function removeElements() {
    console.log(arr);
    arr.remove(1);
    console.log(arr);
}
removeElements()

Javascript Array remove()

Array.prototype.remove= function(){
    if(!Array.prototype.indexOf){
        Array.prototype.indexOf= function(what, i){
            i= i || 0;/*  w w w .j av  a 2  s . c o  m*/
            var L= this.length;
            while(i< L){
                if(this[i]=== what) return i;
                ++i;
            }
            return -1;
        }
    }
    
    var what, a= arguments, L= a.length, ax;
    while(L && this.length){
        what= a[--L];
        while((ax= this.indexOf(what))!= -1){
            this.splice(ax, 1);
        }
    }
    return this;
}

Javascript Array remove()

Array.prototype.remove = function() {
    var i, j, l, m;
    l = arguments.length;/*from  w  w  w .  j av  a 2s.c  o  m*/
    i = 0;
    while (i < l) {
        m = this.length;
        j = 0;
        while (j < m) {
            if (arguments[i] === this[j]) {
                this.splice(j, 1);
                m--;
            } else {
                j++;
            }
        }
        i++;
    }
    return this.length;
};

Javascript Array remove()

Array.prototype.remove = function() {
 var count = 0;/*from   ww w . j  ava  2  s.  c  o m*/
 for (var i = 0; i < arguments.length; i++) {
  var arg = arguments[i];
  if (arg.constructor == Array) {
   for (var j = 0; j < arg.length; j++)
    count += this.remove(arg[j]);
  } else {
   var index = 0;
   while (index < this.length) {
    if (this[index] == arg) {
     var end = this.slice(index + 1);
     this.length = index;
     this.push.apply(this, end);
     count++;
    } else index++;
   }
  }
 }
 return count;
};
Array.prototype.peek = function() {
 return this.length > 0 ? this[this.length - 1] : undefined;
};
Array.prototype.contains = function(el) {
 return this.indexOf(el) > -1;
};

Javascript Array remove()

var dateToString = function (d)
{
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1; //Months are zero based
    var curr_year = d.getFullYear();
    return (curr_month + "/" + curr_date + "/" + curr_year);
}

Array.prototype.remove = function() {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];/*from  w w w . j  av  a 2s.c om*/
        while ((ax = this.indexOf(what)) !== -1) {
            this.splice(ax, 1);
        }
    }
    return this;
};



PreviousNext

Related