Member Login:

Check to see if two arrays are equal

Document Reference: TN201808004 - Rev: 4.01 - Last Update: 01-09-2018 10:32 GMT - Downloaded: 22-Jun-2024 17:16 GMT

The mcrSf.arrEquals() method will compare two JavaScript arrays and return true if both arrays are found to be equal.

This method takes two arguments and returns a Boolean value. The two required arguments pass JavaScript Array Objects to the method. Value true is returned if both arrays are identical.

This can be very complex as arrays can contain any type of elements (string, number, arrays, objects ..) to any number of levels. A check with a comparison operator (e.g. array1 == array2) will compare the memory location of the array, not the array content.


array1Object(Array)First of two arrays to be compared.
array2Object(Array)Second of two arrays to be compared.

Return Value

BooleanReturns true if both arrays are equal.

The Stand-Alone Function

 function arrEquals(array1, array2) {
    return JSON.stringify(array1) === JSON.stringify(array2);

The mcrSf Library Method

* check (compare) two arrays if both are equal
* @version 0.1
* @param {Object(Array)} array1 - first of two arrays to be compared
* @param {Object(Array)} array2 - second of two arrays to be compared
* @return {Boolean} - returns true if both arrays are equal
* mcrSf.arrEquals(["A", "B", "C", "D"], ["A", "B", "C", "D"])
* returns true
* mcrSf.arrEquals(["A", "B", "C", "D"], ["B", "C", "D"])
* returns false
* STRATEGY: Avail of JSON.stringify() and compare strings
arrEquals: function(array1, array2) {
    return JSON.stringify(array1) === JSON.stringify(array2);

Version History

First release, arrEquals().

This website uses cookies to give you the best experience on our website, to personalise content and to analyse our website traffic. Some cookies may have been set already. To find out more about our use of cookies you can visit our Privacy Statement. By browsing this website, you agree to our use of cookies.

Hide this message