简体   繁体   English

寻找不同阵列中的匹配项(Google Apps脚本)

[英]Looking for matches in different arrays (Google Apps Script)

I have the following script in Google Apps Script: 我在Google Apps脚本中有以下脚本:

 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. 2个数组是动态创建的。 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. 所以这个想法是(而且我知道必须有更好的方法来实现这一点,但我对此很新,所以请耐心等待)我将i设置为一个数组中第一个产品代码的值然后循环另一个数组,同时将产品代码存储在这一个中。 Now, I tried logging: 现在,我尝试记录:

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

And this worked and indeed, there were instances where productCode and kelliProduct code matched. 这确实有效,并且确实存在productCode和kelliProduct代码匹配的情况。

Yet my if statement above does not pick these up. 然而我上面的if语句并没有提到这些。

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 ? 要确定哪些prodCodesArr项目也在kelliCodesArr Why not parse kelliCodesArr just once, and then use hash lookups instead of array traversal? 为什么不解析kelliCodesArr一次,然后使用哈希查找而不是数组遍历? This will mean that you don't have to use nested for loops, which will scale very poorly as the inner loop size grows. 这意味着您不必使用嵌套的for循环,随着内部循环大小的增加for循环的扩展性会非常差。 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: 如果___codes数组是2D数组,则应在比较之前将它们展平,因为将Array实例与另一个Array实例进行比较将始终返回false,即使它们包含相同的元素基元 - 它们不是完全相同的Array实例:

References 参考

I'm sure there are more. 我相信还有更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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