jQuery Array Difference : <p>This is isn't really a question, it's a solution but I wanted to post it because I've seen it come up frequently. Feel free to suggest improvements though. I'll update my <a href="https://jsfiddle.net/difster/qvbs6ps4/" rel="nofollow noreferrer">Fiddle</a> with the results.</p>

<p>Using jQuery, this compares 2 arrays and outputs the differences in the two.</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var foo = [];
var i = 0;
jQuery.grep(array2, function(el) {
    if (jQuery.inArray(el, array1) == -1) foo.push(el);
    i++;
});
alert(" the difference is " + foo);</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;</code></pre>
</div>
</div>
</p>
