Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean way to remove element from javascript array (with jQuery, coffeescript)

There are many questions about this, not least: jQuery version of array contains, a solution with the splice method and many more. However, they all seem complicated and annoying.

With the combined powers of javascript, jQuery and coffeescript, what is the very cleanest way to remove an element from a javascript array? We don't know the index in advance. In code:

a = [4,8,2,3]
a.remove(8)     # a is now [4,2,3]

Failing a good built-in method, what is a clean way of extending javascript arrays to support such a method? If it helps, I'm really using arrays as sets. Solutions will ideally work nicely in coffeescript with jQuery support. Also, I couldn't care less about speed, but instead prioritize clear, simple code.

like image 865
Peter Avatar asked Jan 28 '11 07:01

Peter


3 Answers

CoffeeScript:

Array::remove = (e) -> @[t..t] = [] if (t = @indexOf(e)) > -1

Which simply splices out the element at position t, the index where e was found (if it was actually found t > -1). Coffeescript translates this to:

Array.prototype.remove = function(e) {
    var t, _ref;
    if ((t = this.indexOf(e)) > -1) {
        return ([].splice.apply(this, [t, t - t + 1].concat(_ref = [])), _ref);
    }
};

And if you want to remove all matching elements, and return a new array, using CoffeeScript and jQuery:

Array::remove = (v) -> $.grep @,(e)->e!=v

which translates into:

Array.prototype.remove = function(v) {
    return $.grep(this, function(e) {
        return e !== v;
    });
};

Or doing the same without jQuery's grep:

Array::filterOutValue = (v) -> x for x in @ when x!=v

which translates to:

Array.prototype.filterOutValue = function(v) {
    var x, _i, _len, _results;
    _results = [];
    for (_i = 0, _len = this.length; _i < _len; _i++) {
        x = this[_i];
        if (x !== v) {
            _results.push(x);
        }
    }
    return _results;
};
like image 68
Amir Avatar answered Oct 14 '22 13:10

Amir


Using vanilla Javascript:

Array.prototype.remove = function(elem) {
    var match = -1;

    while( (match = this.indexOf(elem)) > -1 ) {
        this.splice(match, 1);
    }
};

var a = [4, 8, 2, 3];

a.remove(8);

Only jQuery:

jQuery.removeFromArray = function(value, arr) {
    return jQuery.grep(arr, function(elem, index) {
        return elem !== value;
    });
};

var a = [4, 8, 2, 3];

a = jQuery.removeFromArray(8, a);
like image 21
jAndy Avatar answered Oct 14 '22 12:10

jAndy


This is really easy with jQuery:

var index = $.inArray("value", myArray);
if(index != -1)
{
  myArray.splice(index, 1);
}

Notes:

splice returns the elements that were removed, so don't do myArray = myArray.splice(). myArray.splice(index,1) means "remove the array element at index 'index' from the array".

$.inArray returns the index in the array of the value you're looking for, or -1 if the value isn't in the array.

like image 28
adavea Avatar answered Oct 14 '22 11:10

adavea