I have developed this method which compares 2 (or more) arrays, and returns whatever you wish as result.
This is done by converting the arrays to strings then comparing them, then converting back the result. (could also be used for other stuff) instead of just doing deep iterations and recursions to do the same thing..
example:
var arr1 = [[8,0,3,0,0,7,0,9,0],[0,9,0,0,3,0,0,0,0],[0,0,0,0,0,0,4,0,6],[0,0,0,0,3,9,7,6,0],[9,6,0,5,0,7,0,8,1],[0,7,4,6,8,0,0,0,0],[5,0,1,0,0,0,0,0,0],[0,0,0,0,5,0,0,7,0],[0,6,0,7,0,0,1,0,8]];
var arr2 = [[8,7,3,0,0,7,0,9,0],[0,9,0,0,3,0,0,0,0],[0,0,0,0,0,0,4,0,6],[0,0,0,0,3,9,7,6,0],[9,6,0,5,0,7,0,8,1],[0,6,4,6,8,0,0,0,0],[5,0,1,0,0,0,0,0,0],[0,0,0,0,5,0,3,7,0],[1,6,0,7,0,0,1,0,8]];
arr1 = JSON.stringify( arr1 );
arr2 = JSON.stringify( arr2 );
var temp = ''; // this object will hold the XOR result
console.log( arr1 );
console.log( arr2 );
for( var i=0; i < arr1.length; i++ ){
if( arr1[i] == '[' || arr1[i] == ']' || arr1[i] == ',' )
temp += arr1[i];
else
temp += arr1[i] == arr2[i] ? 0 : 1;
}
console.log( temp );
what are your thoughts of this method? better for performance I believe.
Here’s a version that works for any type of object:
Or if you want to list the locations of all the differences, you can use something like: