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
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#