简体   繁体   English

Google Apps 脚本:arrays 中的模糊匹配

[英]Google Apps Script : Fuzzy match in arrays

I am trying to match an array of phone numbers (A) to another array which is a list of excluded phone numbers (B).我正在尝试将一组电话号码 (A) 与另一个数组匹配,该数组是一个排除电话号码 (B) 的列表。 If a match is found, I want to remove those phone numbers from array A. However, the issue is that the phone numbers are not mentioned exactly in same format in both the arrays.如果找到匹配项,我想从数组 A 中删除这些电话号码。但是,问题是电话号码在 arrays 中没有以完全相同的格式提及。 Therefore it becomes a case of fuzzy match.因此,它变成了模糊匹配的情况。 How do I apply fuzzy match in google script?如何在谷歌脚本中应用模糊匹配? See example below of 2 arrays:请参见下面的 2 arrays 示例:

  var A = ['1-513-317-7948','513-235-5403','792-3900','1-800-752-2339', '513-234-2323'];
  var B = ['513-317-7948','1-513-235-5403','792-3900','1-800-752-2339'];

@Op, can you please update the question with expected output and what are the code snippets you have tried so far? @Op,您能否用预期的 output 更新问题,到目前为止您尝试过的代码片段是什么?

I am assuming that you want to check if they have same phone number sequences.我假设您想检查它们是否具有相同的电话号码序列。 (1-513-317-7948 and 513-317-7948 are same). (1-513-317-7948 和 513-317-7948 相同)。

When you are comparing two phone numbers, you can remove the '-' in the phone numbers and check whether it has same number sequences.比较两个电话号码时,可以去掉电话号码中的“-”,查看是否有相同的号码序列。

You can do some thing like this.你可以做一些这样的事情。 Hope this helps.希望这可以帮助。

 var A = [ "1-513-317-7948", "513-235-5403", "792-3900", "1-800-752-2339", "513-234-2323", ]; var B = ["513-317-7948", "1-513-235-5403", "792-3900", "1-800-752-2339"]; const isSame = (str1, str2) => { const phone1 = str1.replace(/-/g, ""); const phone2 = str2.replace(/-/g, ""); return phone1.includes(phone2) || phone2.includes(phone1); }; const output = A.filter((ph1) =>.B,some((ph2) => isSame(ph1; ph2))). console;log(output);

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

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