I could not find how to compare two arrays in app script so here is what I came up with (Using the Q&A method of stackoverflow).
I wanted a function that would display the contents of Array 1 that are the same as Array 2 and that are different than Array 2.
If someone has a more efficient way please post.
I find this to be more concise and, if you are doing lots of records, faster. You could speed it up faster if you don't care about keeping your main arrays by just using those instead of duplicating them into same/diff.
It works by deleting from the diff those that have been found as the same and deleting from the same and adding to the diff area those that are different.
//
function exampleCopareArrays(){
var array1 = [1,2,3,5];
var array2 = [5,1,4,6];
var same = array1;
//Set Same array = to 1 we will be deleting here
var diff =array2;
//Set dif array as one we will be adding too.
var matchfound = false;
for (var i=0; i < same.length; i++)
{
matchfound=false;
for (var p=0;p < diff.length;p++)
{
if (same[i] == diff[p])
{
diff.splice(p,1);
matchfound=true;
continue;
}
}
if (matchfound==false)
{
diff.push(same[i]);
same.splice(i, 1)
i=i-1;
}
}
same.sort();
diff.sort();
Logger.log(same);
Logger.log(diff);;
}
// exame functionality with the Comapre script
function exampleCopareArrays(){
var array1 = [1,2,3,5];
var array2 = [5,1,4,6];
var compared = compareArrays(array1,array2);
var theDiff = compared[0];
var theSame = compared[1];
}
// This will compare 2 Arrays
function compareArrays(a1, a2){
//var a1 = [1,2,3,5];
//var a2 = [5,1,4,6];
var a1L = a1.length-1;
var a2L = a2.length-1;
var aDiff = [];
var aSame = [];
var tf = "FALSE";
for (var j=0; j <= a1L; ++j) {
var a1Value = a1[j];
for (var k=0 ; k <= a2L; k++) {
var a2Value = a2[k];
if (a2Value == a1Value ){tf = "TRUE"; k= a2L; }
} // end for a2L
if (tf != "TRUE"){
aDiff.push(a1Value);}
else {
aSame.push(a1Value);}
tf = "FALSE";
} // end for a1L
var theArrays = [];
theArrays.push(aDiff);
theArrays.push(aSame);
//var theArrays = [[aDiff],[aSame]];
//Logger.log(theArrays);
return(theArrays);
} // end compareArrays
Thanks Kevrone. I made one modification so that it didn't merge the two different arrays, but rather just listed the same and different numbers, which now matches my original script.
function compareArrays2(array1, array2){
var array1 = [1,3,2,5];
var array2 = [5,1,4,6];
var same = array1;
//Set Same array = to 1 we will be deleting here
var diff =array2;
//Set dif array as one we will be adding too.
var matchfound = false;
for (var i=0; i < same.length; i++)
{
matchfound=false;
for (var p=0;p < diff.length;p++)
{
if (same[i] == diff[p])
{
diff.splice(p,1);
matchfound=true;
continue;
}
}
if (matchfound==false)
{
// diff.push(same[i]); //Put this back in if you want to merge the numbers that are different.
same.splice(i, 1)
i=i-1;
}
}
same.sort();
diff.sort();
// Logger.log(same);
// Logger.log(diff);
var theArrays = [];
theArrays.push(diff);
theArrays.push(same);
//var = theArrays[] = [[aDiff],[aSame]];
Logger.log(theArrays);
return(theArrays);
} // end compareArrays