Checking for duplicates using includes/indexOf doesnt work in Google App Script

I am trying to look for duplicate from a range (array) of new data and flag them into the spreadsheet as duplicate when it an existing data is available from another range.

However, my problem is that the conditions where the new data is compared to the existing list of data is not working. I used indexOf and includes function to no avail.

New data example (printed from the Logger):

joNumRangeUploader:
77324,77320,77318,77341,77302,77267,76975,76576

Existing list example data (printed from the Logger):

joNumRangeMMICS:
77123,77002,77020,77037,77041,77042,77125,75861,74869,74960,75971,76608,76508,77287,75192,75751,75397,75715,76609,76610,74910,74814,77158,75484,76358,75984,75932,75931,76940,76444,76682,76970,76910,76459,76974,76975,76576,

Expected result:

76975,76576 should both be flagged as duplicates

Actual result:

But, then again, they are not flagged nor the condition with includes() work.

Below is my code:

//get values into array
const joNumRangeUploader = uploaderSheet.getRange(UploaderFirstDataRow,joNumColUploader,uploaderRowCount,1).getValues();
const joNumRangeMMICS    = mmicsSheet.getRange(2,joNumColSource,mmicsSheet.getLastRow(),1).getValues();
const joNumRangeBAU      = bauSheet.getRange(2,joNumColSource,bauSheet.getLastRow(),1).getValues();

let uniqueDataArray = new Array();

//go through joNumRangeUploader array and check for a duplicate; write to cell if duplicate
for(i=0; i < joNumRangeUploader.length; i++){
    const dataToCheck = joNumRangeUploader[i][0];
    let isUnique = true;

    let fileSource = '';
    //look for duplicates compating the dataToCheck to the 2 other indices
    if(joNumRangeMMICS.includes(dataToCheck)){
      fileSource = 'MMICS';
      isUnique = false;
      ui.alert("Duplicate found!");   //doesnt show in actual
    }
    else if(joNumRangeBAU.includes(dataToCheck) && isUnique === true){
      fileSource = 'MASTERFILE';
      isUnique = false;
      ui.alert("Duplicate found!");   //doesnt show in actual
    }

    if (isUnique==true){
      const pushRange = uploaderSheet.getRange((i+UploaderFirstDataRow),firstColUpload, 1, sourceColCount);
      //push upload range to array
      uniqueDataArray.push(pushRange.getValue());
      //expected: Lx:ANx
    }
    else{
      uploaderSheet.getRange( (i+UploaderFirstDataRow), statusCol, 1, 1).setValue("DUPLICATE " + fileSource);
    }
}

Answer

For the sake of completeness, I’m posting the solution to your original problem (cannot find duplicates between two arrays), even though you found a workaround via formulas.

Issue:

Both joNumRangeUploader and joNumRangeMMICS are 2D arrays (that’s what getValues() returns), and you’re treating them as simple arrays.

Solution:

Use flat() after calling getValues() to get simple arrays instead:

const joNumRangeUploader = uploaderSheet.getRange(UploaderFirstDataRow,joNumColUploader,uploaderRowCount,1).getValues().flat();
const joNumRangeMMICS    = mmicsSheet.getRange(2,joNumColSource,mmicsSheet.getLastRow(),1).getValues().flat();