jQuery Compare Two Arrays
Determine if two arrays have the same elements in them.
by Vitzkrieg
HTML
<div id="result1"></div>
<div id="result2"></div>
JavaScript
var array1 = [1,2,3,4,5],
array2 = [5,4,3,2,1],
array3 = [1,2,,6,7,8];
function compareArrays(arr1, arr2) {
return $(arr1).not(arr2).length == 0 && $(arr2).not(arr1).length == 0
};
$('#result1').html(compareArrays(array1, array2) ? 'true' : 'false');
$('#result2').html(compareArrays(array2, array1) ? 'true' : 'false');
$('#result3').html(compareArrays(array1, array3) ? 'true' : 'false');
$('#result3').html(compareArrays(array2, array3) ? 'true' : 'false');