JSFiddle - React, Tailwind, and code Playground
by Pratik Bhonsle
HTML
<input type="button" onclick="showTests()" value="showTests" />
<pre id="tests">
</pre>
JavaScript
Array.prototype.equals = function (array, sort) {
// if the other array is a falsy value, return
if (!array) {
return false;
}
// compare lengths - can save a lot of time
if (this.length != array.length) {
return false;
}
if(sort){
this.sort();
array.sort();
}
for (var i = 0; i < this.length; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && array[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(array[i])) {
return false;
}
} else if (this[i] != array[i]) {
// Warning - two different object instances will never be equal:
// {x:20} != {x:20}
return false;
}
}
return true;
};
function showTests() {
var pre = document.getElementById("tests");
var tests = [
[ [1,1,2,6], [1,1,2,6] ], // true
[ [1,2,6], [1,1,2,6] ], // false
[ [1,6], [6,1] ], // true
[ ['a',1,6], ['a',1,6] ], // true
[ [1, [1, 2, [3, 2]], [7, [3, 2], 7], 2, [3, 2]], [[6, [3, 2], 7], [1, 2, [3, 2]],1, 2, [3, 2]] ]
];
for(var i = 0; i < tests.length; i++){
pre.innerHTML += i + ' ' + tests[i][0].equals(tests[i][1]) + '<br/>';
}
pre.innerHTML += 'Sorted <br/>';
for(var i = 0; i < tests.length; i++){
pre.innerHTML += i + ' ' + tests[i][0].equals(tests[i][1], true) + '<br/>';
}
}