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.
Parameters
Parameter | Type | Description |
---|---|---|
array1 | Object(Array) | First of two arrays to be compared. |
array2 | Object(Array) | Second of two arrays to be compared. |
Return Value
Type | Description |
---|---|
Boolean | Returns 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
Revision Date | Details |
---|---|
0.1 01-Sep-18 | First release, arrEquals() . |