简体   繁体   中英

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). 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. Therefore it becomes a case of fuzzy match. How do I apply fuzzy match in google script? See example below of 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?

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

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

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