Say I have two arrays
["a", "b", "c"]
["c", "a", "b"]
What is the best way to compare these two arrays and see if they are equal (they should come as equal for the above scenario)
Say I have two arrays
["a", "b", "c"]
["c", "a", "b"]
What is the best way to compare these two arrays and see if they are equal (they should come as equal for the above scenario)
function compareArrays(array1, array2) {
    array1 = array1.slice();
    array2 = array2.slice();
    if (array1.length === array2.length) {       // Check if the lengths are same
        array1.sort();
        array2.sort();                           // Sort both the arrays
        return array1.every(function(item, index) {
            return item === array2[index];       // Check elements at every index
        });                                      // are the same
    }
    return false;
}
console.assert(compareArrays(["a", "b", "c"], ["c", "a", "b"]) === true);
 
    
    You can try with  _.difference
var diff = _(array1).difference(array2);
if(diff.length > 0) {
    // There is a difference
}
this will not work because different returns diff from first array. _.difference(['a'] ,['a','b']) is 0 but two array is not equal.
 
    
    