Nodejs Array Remove Object remove(value)

Here you can find the source of remove(value)

Method Source Code

//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 (value) {
    //from   ww  w .j a v a2 s  .  co  m
    while (this.indexOf(value) >= 0) {
        this.splice(this.indexOf(value), 1);
    }
    return this;
}

//Another possible solution using for construction
Array.prototype.remove2 = function (value){
    var i, len,
        possitions = [];

    for (i = 0, len = this.length; i < len; i += 1) {
        if (value === this[i]) {
            possitions.push(i);
        }
    }

    for (i = possitions.length - 1; 0 <= i; i -= 1) {
        this.splice(possitions[i], 1);
    }
    return this;
}

//test
var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
console.log('Original array: ' + arr);
console.log('Array after removing 1: ' + arr.remove(1));
console.log('NOTE that the last "1" is a string, therefore is not removed');

Related

  1. remove(value)
    Array.prototype.remove = function(value) {
        var index = this.indexOf(value);
        if (index != -1) {
            return this.splice(index, 1); 
        return false;
    
  2. remove(value)
    Array.prototype.remove = function (value) {
        for (var i = 0, len = this.length; i < len; i += 1) {
            if (this[i] === value) {
                this.splice(i, 1);
        return this;
    };
    var arr = [1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 11, '1'];
    ...
    
  3. remove(value)
    Array.prototype.remove = function (value) {
        var idx = this.indexOf(value);
        if (idx !== -1) {
            return this.splice(idx, 1);
        return false;
    };
    
  4. remove(value)
    Array.prototype.remove = function (value) {
        var arr = [];
        for(var i = 0, len = this.length; i < len; i++) {
            if(this[i] !== value) {
                arr.push(this[i]);
        for(var i = 0, len = this.length; i < len; i++) {
            this.pop();
    ...
    
  5. remove(value)
    Array.prototype.remove = (value) => {
        const position = this.indexOf(value)
        if (~position) this.splice(position, 1)
        return this
    
  6. remove(value)
    Array.prototype.remove = function(value) {
        for (var i = 0; i < this.length; i+=1) {
            if (this[i] === value) {
                this.splice(i, 1)
                i-=1;
    
  7. remove(value)
    Array.prototype.remove = function(value) {
      var idx;
      for (idx = this.indexOf(value); idx > -1; idx = this.indexOf(value)) {
        this.splice(idx, 1);
      return this;
    };
    function test() {
      var i, len, N, value, result, arr = [];
    ...
    
  8. remove(value)
    Array.prototype.remove = function(value)
      for (var i = 0; i < this.length; i++)
        if (this[i] == value)
          this.splice(i, 1);
          return;
    
  9. remove(values)
    Array.prototype.remove = function(values){
      return this.filter(function(v){
        if(sb.typeOf(values) !='array'){
          return v != values;
        } else {
          return !values(v);
      });
    };
    ...