var aSum = ['a', 'b', 'c', 'd', 'e', 'f ', 'g' ]; var aRemove = ['c', 'f', 'g' ];
desired aResult : ['a', 'b', 'd', 'e']
I'm interested in finding out what would be the fastest way to accomplish this, provided that a,b,c, etc would be arbitrary strings. There's of course various approaches but I can't find info on what would be the speediest.
Perhaps there's a stupifying hiatus in my array knowledge, if so I'm eager to learn 
Atm I'm leaning towards aSum.join(), searching and removing string matches while iterating over aRemove, then split() again, but it seems convoluted to me.
is this what you want HOT XXX Array.diff PORN
Thanks, that's very straightforward and works fine for what I'm doing. I was just wondering if there was some insanely clever way to do this that I wasn't aware of 
An alternative approach:
Array.prototype.diff = function(a) { return this.filter(function(i) {return !(a.indexOf(i) > -1);}); };
http://stackoverflow.com/questions/1187518/javascript-array-difference
Personally I'd avoid adding methods to the prototype of Array (because if you're not careful, it can cause problems with iterating over arrays), but it seems like a nice approach. Have no idea if it's faster than Techno's suggestion, though. :shrug:
I could use it without prototyping, but then I'd still need to add the filter method to the array prototype for older browsers afaik - interesting suggestion though 
I went with the nested loops and it's ok. Fwiw it was used for populating a bunch of selects on a page with only the options that weren't taken yet by other selects.
Just to expand on stickman's concerns...
IndexOf and filter are not available in IE arrays.
You'd need to add each of these methods in the case of IE to use the stackflow approach.
This page has an ie compatibility fix http://www.tutorialspoint.com/javascript/array_filter.htm
BUT DON'T DO THIS. The stackflow solution will only cause major issues. It's nifty in chrome, running in ms on large arrays, but it's satan.
It's totally breaking IE when you add prototypes to array. The reason is that Proto in ie is all a big lie. When you iterate over an array in ie using a for in loop the methods will appear as members of the array. If you try to use associative or named arrays after extending prototype, the member difference will cause sorting, and length inconsistencies.
If you bury this in some library full of proto fixes and additions it can make it hell for a developer to debug is own code.