Nodejs Array Remove by Value removeItem(value)

Here you can find the source of removeItem(value)

Method Source Code

Array.prototype.removeItem = function (value) {
    var items = this.slice(0),
        newArray = [],/*www .  j  a  v  a 2s.c  o  m*/
        i;

    for(i = 0; i < items.length; i++) {
        if(items[i] === value) {
            continue;
        }
        newArray.push(items[i]);
    }

    return newArray;
};

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

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

Related

  1. removeItem(value)
    Array.prototype.removeItem = function(value) {
        var result = [];
        for (var i = 0; i < this.length; i++) {
            if (this[i] === value) {
                this.splice(i, 1);
        return this;
    };
    ...
    
  2. removeItem(value)
    Array.prototype.removeItem = function (value) {
        var result = [];
        for (var i = 0; i < this.length; i++) {
            if (this[i] === value) {
                this.splice(i, 1); 
        return this;
    };
    ...
    
  3. removeItem(value)
    Array.prototype.removeItem = function (value) {
        var len = this.length;
        var resultArr = [];
        for (var i = 0; i < len; i++) {
            if (this[i] !== value) {
                resultArr.push(this[i]);
        return resultArr;
    ...
    
  4. removeItem(value)
    Array.prototype.removeItem = function(value) {
        for (var i = 0; i < this.length; i++) {
            if(this[i] === value) {
                this.splice(i, 1); 
    };
    var arrFirst = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
    arrFirst.removeItem(1);
    ...
    
  5. 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);
    
  6. 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' ];
    ...
    
  7. 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;
    };
    ...
    
  8. 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'));
    ...
    
  9. removeItem(value)
    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);
    ...