Skip to main content
4 of 4
added 35 characters in body
machineghost
  • 36k
  • 33
  • 174
  • 271

jQuery does not have a method for comparing arrays. However the Underscore library (or the comparable Lodash library) does have such a method: isEqual, and it can handle a variety of other cases (like object literals) as well. To stick to the provided example:

var a=[1,2,3]; var b=[3,2,1]; var c=new Array(1,2,3); alert(_.isEqual(a, b) + "|" + _.isEqual(b, c)); 

By the way: Underscore has lots of other methods that jQuery is missing as well, so it's a great complement to jQuery.

EDIT: As has been pointed out in the comments, the above now only works if both arrays have their elements in the same order, ie.:

_.isEqual([1,2,3], [1,2,3]); // true _.isEqual([1,2,3], [3,2,1]); // false 

Fortunately Javascript has a built in method for for solving this exact problem, sort:

_.isEqual([1,2,3].sort(), [3,2,1].sort()); // true 
machineghost
  • 36k
  • 33
  • 174
  • 271