Nodejs Array Remove Item remove(numToRemove)

Here you can find the source of remove(numToRemove)

Method Source Code

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

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


Array.prototype.remove = function(numToRemove) {
    var i;//w  w  w  .  j  a v  a2 s.  com


    for (i = 0; i < this.length; i++) {
        if (this[i] === numToRemove) {
            this.splice(i, 1);
            i--;
        }
    }



};
arr.remove(1);
console.log(arr);

Related

  1. remove(item)
    Array.prototype.remove = function(item) {
      var newArray = [];
      if (this.indexOf(item) === -1) {
        return this;
      } else {
        for (var i = 0; i < this.length; i++) {
          if (this[i] !== item) {
            newArray.push(this[i]);
      return newArray;
    };
    
  2. remove(item)
    Array.prototype.remove = function(item)
      var p = this.indexOf(item);
      if(p != -1)
        this.splice(p,1);
    };
    
  3. remove(item)
    Array.prototype.remove = function(item){
      if (this.contains(item)) return this.splice(this.indexOf(item), 1);
    
  4. remove(itemToRemove)
    Array.prototype.remove = function (itemToRemove) {
        var i,
            len = this.length;
        for (i = 0; i < len; i += 1) {
            if (this[i] === itemToRemove) {
                this.splice(i, 1);
    };
    ...
    
  5. remove(num)
    Array.prototype.remove = function(num) {
      this[num] = null;
      return this;
    };
    
  6. remove(number)
    function run() {
        var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, "1"];
        jsConsole.writeLine("The initial array: " + arr);
        arr.remove(1); 
        jsConsole.writeLine("The array with the number 1 removed: " + arr);
    Array.prototype.remove = function (number) {
        for (var i = 0; i < this.length; i++) {
            if (number === this[i]) {
    ...
    
  7. remove(number)
    var example = [1, 4, 5, 3, 6, 4, 7, 3, 2];
    Array.prototype.remove = function(number){
        var editedArray = [],
            index;
        for (index = 0; index < this.length; index += 1) {
            if (this[index] != number) {
                editedArray.push(this[index]);
        return editedArray;
    };
    console.log(example);
    console.log(example.remove(4));
    
  8. remove(numberToRemove)
    Array.prototype.remove = function(numberToRemove) {
        var index,
            len = this.length;
        for (index = 0; index < len; index += 1) {
            if (this[index] === numberToRemove) {
                this.splice(index, 1);
                index -= 1;
    };
    var arr = [1, 2, 5, 54, 32, 1, 4, 4, 1, 1, 1, 54, 3, 65, 4];
    arr.remove(1);
    console.log(arr);
    
  9. removeItem( (item)
    Array.prototype.removeItem = (function (item){
        var newArray = [];
        for (var index in this) {
            if(this[index] !== item && typeof this[index] !== "function") {
                newArray.push(this[index]);
        return newArray;
    });
    ...