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]
Written by Lee Crossley
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Diff
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#