Nodejs Array Remove Object remove(value)

Here you can find the source of remove(value)

Method Source Code

/*//from 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 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'];
 */

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 = [];

   len = 10; // length of test array
   N = 10; // Maximal number in the array
   value = (len*Math.random()) | 0; // number to be searched

   // Populate the array arr with random numbers
   for (i = 0; i < len; i += 1) {
      arr[i] = (N*Math.random()) | 0;
   }

   console.log('array: [' + arr.join(', ') + ']');
   console.log(value);
   console.log('array: [' + arr.remove(value).join(', ') + ']');

   console.log();


   arr = [1,2,1,4,1,3,4,1,111,3,2,1,'1'];
   value = 1;
   console.log('array: [' + arr.join(', ') + ']');
   console.log(value);
   console.log('array: [' + arr.remove(value).join(', ') + ']');
}


test();

Related

  1. remove(value)
    Array.prototype.remove = function (value) {
        var idx = this.indexOf(value);
        if (idx !== -1) {
            return this.splice(idx, 1);
        return false;
    };
    
  2. 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();
    ...
    
  3. remove(value)
    Array.prototype.remove = (value) => {
        const position = this.indexOf(value)
        if (~position) this.splice(position, 1)
        return this
    
  4. remove(value)
    Array.prototype.remove = function (value) {
        while (this.indexOf(value) >= 0) {
            this.splice(this.indexOf(value), 1);
        return this;
    Array.prototype.remove2 = function (value){
        var i, len,
            possitions = [];
    ...
    
  5. 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;
    
  6. remove(value)
    Array.prototype.remove = function(value)
      for (var i = 0; i < this.length; i++)
        if (this[i] == value)
          this.splice(i, 1);
          return;
    
  7. remove(values)
    Array.prototype.remove = function(values){
      return this.filter(function(v){
        if(sb.typeOf(values) !='array'){
          return v != values;
        } else {
          return !values(v);
      });
    };
    ...
    
  8. remove(what)
    Array.prototype.remove = function(what) {
       var index = this.indexOf(what);
       if (index !== -1) {
          this.splice(this.indexOf(what), 1);
       return this;
    
  9. remove(x)
    Array.prototype.remove = function (x) {
      var keepers = [];
      for (var i = 0; i < this.length; i += 1) {
        if (this[i] !== x) {
          keepers.push(this[i]);
      return keepers;
    };
    ...