Array Comparison

Let’s say you want to compare 2 arrays with what I call a 1-1 comparison.  So if you had:

var array1 = [1,2,3,3];

var array2 = [1,2,3,4];

The comparison would tell you about the extra 3 in the first array and the 4 in the second array that doesn’t appear in the first array.  After searching online I found many array comparisons, but none that did that, so I wrote the one below.

The a.slice() with no value in the slice parameter slices at the zero position, so it’s basically just making a copy of the array so the original array will be in tact for other operations (read: the second comparison).  The return will give you an array of the results, and I’ve added 2 comments to help with the readability.  Using the two arrays from the example above, the return would be [4,3].

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">