onit
onit

Reputation: 2372

Why is this array comparison not working in Google Sheets (using Apps Script)?

I'm used to comparing arrays like this, but this is not working as expected this time: The data compared is different, but it keeps saying it is duplicated. enter image description here

Here's the code I'm using:

function salvarPrevVendas() {
  const lock = LockService.getScriptLock();
  lock.tryLock(3000);
  if (lock.hasLock()) {
    var sourceSheet = 'PCP';
    var destinationSheet = 'PrevVendasDB';
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName(sourceSheet);
    var LastRowSource = sheet.getLastRow();
    var values = sheet.getRange(40, 1, 57, 10).getValues();
    var csh = ss.getSheetByName(destinationSheet);
    var data = new Array();
    var dbData = csh.getRange(2, 1, csh.getLastRow(), 10).getValues();
    var pushedDbData = new Array();
    var date = sheet.getRange("I34").getValue();
    var vendedor = sheet.getRange("I35").getValue();

    //Loops through the range of items in the hidden columns and pushes these data into an array (data);
    for (var i = 0; i < values.length; i++) {
      if (values[i][0] != '') {
        data.push(values[i]);
      }
    }
     
    //Loops through the DB sheet and pushes these data into an array (pushedDbData) to be compared later;
    for (var n = 0; n < dbData.length; n++) {
      //Logger.log("Valor da Data no DB: " + dbData[n][0].valueOf())
      if (dbData[n][0].valueOf() == date.valueOf() && dbData[n][1] == vendedor) {
        pushedDbData.push(dbData[n]);
      }
    }    

var duplicate = false;
    loop1:
    for (var x = 0; x < data.length; x++) {
      loop2:
      for (var j = 0; j < pushedDbData.length; j++) {
        if (data[x].join() == pushedDbData[j].join()) {
          duplicate = true;
          break loop1;
        }
      }
    }

I have also tried a different approach, but it gives me the same result:

var obj = data.reduce((o, r) => Object.assign(o, { [JSON.stringify(r.join())]: true }), {});
    var duplicate = "";
    if (pushedDbData.length < 1) {
      duplicate = false
    } else {
      duplicate = pushedDbData.every(e => obj[JSON.stringify(e.join())]);
    }

Expected result: duplicate = false

Thanks in advance for your help.

Upvotes: 1

Views: 101

Answers (1)

Yuri Khristich
Yuri Khristich

Reputation: 14537

Your comparing:

var duplicate = false;
    loop1:
    for (var x = 0; x < data.length; x++) {
      loop2:
      for (var j = 0; j < pushedDbData.length; j++) {
        if (data[x].join() == pushedDbData[j].join()) {
          duplicate = true;
          break loop1;
        }
      }
    }

works just fine. Your original data has difference in column H. The comparing halts the loop and value duplicate is false.

enter image description here

I tried to add "1045" in H40 and the value duplicate became true. Which is a correct result of the comparing.

enter image description here

But it eludes me, what should the script do? Now it works fine, as far as I can see.

Update (tests)

// this is exactly your code

function has_duplicates(data, pushedDbData) {

    var duplicate = false;
    loop1:
    for (var x = 0; x < data.length; x++) {
        loop2:
        for (var j = 0; j < pushedDbData.length; j++) {
            if (data[x].join() == pushedDbData[j].join()) {
                duplicate = true;
                break loop1;
            }
        }
    }
    return duplicate
}

arr1 = [['a'], ['b'], ['c']];
arr2 = [...arr1];             // arr2 = arr1
arr3 = [...arr2, ['d']];      // "add row" ['d'] to arr2
arr4 = [['e'], ['f'], ['g']]; // arr4 != arr1
arr5 = [...arr4, ['a']];      // "add row" ['a'] to arr4
arr6 = [...arr4, ['h']];      // "add row" ['h'] to arr4

console.log(has_duplicates(arr1, arr2)); // true
console.log(has_duplicates(arr1, arr3)); // true
console.log(has_duplicates(arr1, arr4)); // false
console.log(has_duplicates(arr1, arr5)); // true
console.log(has_duplicates(arr1, arr6)); // false

It works.

Sorry, I don't understand what are you trying to get.

Upvotes: 2

Related Questions