简体   繁体   中英

Looking for matches in different arrays (Google Apps Script)

I have the following script in Google Apps Script:

 for(var i=0; i<lastCode; i++) {
  var productCode = prodCodesArr[i];
    for(var j=0; j<kelliLastCode; j++) {
     var kelliProductCode = kelliCodesArr[j];
     if(productCode == kelliProductCode) {

     Logger.log('match found')
     }
     }        
  }

The 2 arrays are created dynamically. So the idea is (and I know there must be MUCH better ways to do this, but I am pretty new to this so bear with me) that I am setting i to the value of the first product code in one array and then looping through the other array whilst storing the product codes in this one to j. Now, I tried logging:

Logger.log(productCode + ' - ' + kelliProductCode);

And this worked and indeed, there were instances where productCode and kelliProduct code matched.

Yet my if statement above does not pick these up.

Again, I'm sure I've botched this entirely but any help would be greatly appreciated...

Something like this might help you to see what's happening:

function compareA(prodCodesArr,kelliCodesArr) {
  var html="";
  for(var i=0;i<prodCodesArr.length;i++) {
    for(var j=0;j<kelliCodesArr.length;j++) {
      if(productCodesArr[i]==kelliCodesArr[j]) {
        html+=Utilities.formatString('Matched: %s=%s', productCodesArr[i],kelliCodesArr[j]);
      }else{
        html+=Utilities.formatString('No-Match: %s=%s', productCodesArr[i],kelliCodesArr[j]);
      }                             
    }        
  }
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Comparing')
}

What's the point of the check? To determine which of your prodCodesArr items are also in kelliCodesArr ? Why not parse kelliCodesArr just once, and then use hash lookups instead of array traversal? This will mean that you don't have to use nested for loops, which will scale very poorly as the inner loop size grows. An example (with some checks for assumptions on my part):

function foo() {
  const kelliCodes = getKelliCodesArraySomehow();
  const productCodes = getProductCodesArraySomehow();

  // If these are 2D arrays, note that for `var a = ['help']; var b = ['help'];`
  // `a` is never equal to `b` because they are not the exact same object in memory.
  if (kelliCodes.length && Array.isArray(kelliCodes[0])) {
    throw new TypeError("This SO answer was predicated on `kelliCodes` and `productCodes` being 1D arrays, but they aren't!");
  }

  const kelliLookup = kelliCodes.reduce(function (obj, kpc, idx) {
    if (typeof kpc === 'object') {
      console.log({message: "This SO answer assumed kpc was a string", kpc: kpc});
      throw new TypeError("You probably want to store a property of this object, not the whole object");
    }
    obj[kpc] = idx;
    return obj;
  }, {});

  var productsAlsoInKelliCodes = productCodes.filter(function (pc) {
    return kelliLookup.hasOwnProperty(pc);
  });
  productsAlsoInKelliCodes.forEach(function (pc) {
    Logger.log("The index of this product code %s in kelliCodes is %s", pc, kelliLookup[pc]);
  });
}

If your ___codes arrays are 2D arrays, you should flatten them before comparison, as comparing an Array instance to another Array instance will always return false, even if they contain the same element primitives--they aren't the exact same Array instance:

References

I'm sure there are more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM