Compare 2 arrays which returns difference

Howdy_McGee picture Howdy_McGee · Jun 7, 2012 · Viewed 117.4k times · Source

What's the fastest/best way to compare two arrays and return the difference? Much like array_diff in PHP. Is there an easy function or am I going to have to create one via each()? or a foreach loop?

Answer

superphonic picture superphonic · Mar 13, 2013

I know this is an old question, but I thought I would share this little trick.

var diff = $(old_array).not(new_array).get();

diff now contains what was in old_array that is not in new_array