Nodejs Array Remove by Value removeItem(value)

Here you can find the source of removeItem(value)

Method Source Code

/**/*from  ww w .  j a  v a 2  s .co m*/
 * Write a JavaScript function removeItem(value) that accept as parameter a number or string.
 * The function should remove all elements with the given value from an array.
 * Attach the function to the Array type.
 * You may need to read about prototypes in JavaScript and how to attach methods to object types.
 * You should return as a result the modified array.
 */




Array.prototype.removeItem = function (value) {
    for(var i = this.length-1; i--;){
        if (this[i] === value) {
            this.splice(i, 1)
        }
    }
};

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

var arr1 = ['hi', 'bye', 'hello' ];
arr1.removeItem('bye');
console.log(arr1);

Related

  1. removeItem(value)
    "use script"
    Array.prototype.removeItem = function(value) {
        if(typeof value === 'number' || typeof value === 'string') {
            for (var index in this) {
                if(this[index] === value) {
                    this.splice(index, 1);
    };
    var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
    arr.removeItem(1);
    console.log(arr);
    arr = ['hi', 'bye', 'hello' ];
    arr.removeItem('bye');
    console.log(arr);
    
  2. removeItem(value)
    Array.prototype.removeItem = function (value) {
        var items = this.slice(0),
            newArray = [],
            i;
        for(i = 0; i < items.length; i++) {
            if(items[i] === value) {
                continue;
            newArray.push(items[i]);
    ...
    
  3. removeItem(value)
    "use strict";
    Array.prototype.removeItem = function (value) {
        for (var i in this) {
            if(this[i] === value) {
                this.splice(i, 1);
    };
    var arr = ['hi', 'bye', 'hello' ];
    ...
    
  4. removeItem(value)
    Array.prototype.removeItem = function (value) {
        for (var i = 0; i < this.length; i++) {
            var currElem = this[i];
            if (currElem === value) {
                this.splice(i, 1);
        return this;
    };
    ...
    
  5. removeItem(value)
    Array.prototype.removeItem = function (value) {
        return this.filter(function (v) {
            return v !== value;
        })
    };
    var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
    console.log(arr.removeItem(1));
    arr = ['hi', 'bye', 'hello' ];
    console.log(arr.removeItem('bye'));
    ...
    
  6. removeValue(int)
    Array.prototype.removeValue = function(int) {
      var ans = [];
      var count = 0;
      for (i = 0; i < this.length; i++) {
        if (this[i] !== int) {
          ans.push(this[i]);
        } else if (this[i] === int) {
          count++;
      if (count === 0) {
        return false;
      return ans;
    
  7. removeValue(name, value)
    Array.prototype.removeValue = function(name, value){
      try
       var array = $.map(this, function(v,i){
          return v[name] === value ? null : v;
       });
       this.length = 0; 
       this.push.apply(this, array); 
      catch(e)
        console.log(e);
    
  8. removeValue(name, value)
    Array.prototype.removeValue = function(name, value) {
        var array = $.map(this, function(v, i) {
            return v[name] === value ? null : v;
        });
        this.length = 0; 
        this.push.apply(this, array); 
        return this;
    
  9. removeValue(name, value)
    const DATE_FORMAT = 'Do MMM YYYY';
    const DATETIME_FORMAT = 'Do MMM YYYY LT';
    Array.prototype.removeValue = function(name, value){
       var array = $.map(this, function(v,i){
          return v[name] === value ? null : v;
       });
       this.length = 0; 
       this.push.apply(this, array);