difference between two arrays in javascript

something like so:

Array.prototype.difference = function(other) {
  var a = this.inject($H(), function(h, v) {
    h.set(v, true);
    return h;
  });
 
  other.each(function(v) {
    this.unset(v);
  }.bind(a));
 
  return a.keys();
}

which lets you do this:

>>> [2,3,4].difference([5,6,7])
2,3,4
>>> [2,3,4].difference([5,6,7,4])
2,3
>>> [2,3,4].difference([2,3,4])
[]

About this entry