javascript find difference between two array

0 votes
asked Jul 29, 2014 in JQuery by anonymous

I have two javascript array objects. Is there any easiest way to find whether those two array are same or are there any difference.

For example if I have

var oldPositions = [1, 2 ,5 ,7]
var newPositions = [2 ,7]

or

var oldPositions = [2 ,5 ]
var newPositions = [2 ,5, 9]

in both of these example, I should compare the arrays and find the result as not matched. Any suggestion?

Share

1 Answer

+1 vote
answered Jul 29, 2014 by anonymous

Easiest way for your scenario I could think of is

var DoesArrayMatch = function(oldPositions, newPositions)
var diff1 = jQuery(oldPositions).not(newPositions).get();
var diff2 = jQuery(newPositions).not(oldPositions).get();
if(jQuery.isEmptyObject(diff1) && jQuery.isEmptyObject(diff2))
{
     return true;
}

return false;

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...