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):


Existing list example data (printed from the Logger):


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
      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
      //expected: Lx:ANx
      uploaderSheet.getRange( (i+UploaderFirstDataRow), statusCol, 1, 1).setValue("DUPLICATE " + fileSource);


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.


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


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();