Last Updated: February 25, 2016
·
20.99K
· leecrossley

Difference Between 2 JavaScript Arrays

If your target browser(s) support filter and indexOf (essentially IE9+):

Array.prototype.diff = function (a) {
    return this.filter(function (i) {
        return a.indexOf(i) === -1;
    });
};

Then usage is as simple as:

[1, 2, 3, 4, 5, 6].diff([2, 4, 6]);
// => [1, 3, 5]