jolj4q
Last Updated: February 25, 2016
·
2.006K
· leecrossley
57e15d6acbd5f46c300a83b5e61db1de

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]
Say Thanks
Respond