简体   繁体   English

比较两个数组并删除部分匹配的项目

[英]Compare two arrays and remove items with partial match

I need to compare two arrays for PARTIAL / SUBSTRING matches.我需要比较 PARTIAL / SUBSTRING 匹配的两个数组。 If these are found, I want the matching items removed from the originalArray or create a new array with all those items that did not have a partial match,如果找到这些,我希望从 originalArray 中删除匹配的项目,或者创建一个包含所有没有部分匹配的项目的新数组,

var originalArray = ['www.google.com/opq', 'www.yelp.com/abc', 'www.yahoo.com/klm', 'www.bing.com/xyz', 'www.google.com/124'];

var itemsToBeRemoved = ['google.com', 'yahoo.com'];

// DESIRED ARRAY: An array with items that did not have a partial match. For example: 
var cleanArray = ['www.yelp.com/abc', 'www.bing.com/xyz']

It work when its a FULL / EXACT match.它在完全/完全匹配时工作。 But it is NOT what I need.但这不是我需要的。

function cleanFullMatch(){ 
// Your main array  
var originalArray = ['www.google.com/rugby', 'www.yelp.com/abc', 'www.yahoo.com/tennis', 'www.bing.com/xyz', 'www.google.com/football']; 
Logger.log(originalArray);
    
// This array contains strings that needs to be removed from main array 
var itemsToBeRemoved = ['www.google.com/rugby', 'www.yahoo.com/tennis', 'www.google.com/football'];

var cleanObj = {};
itemsToBeRemoved.forEach( e => cleanObj[e] = true); 
itemsToBeRemoved.forEach( e => itemsToBeRemoved[e]= true);
    
var cleanArray = originalArray.filter(function(val){   return !cleanObj [val] })
Logger.log(cleanArray);
}

You can use JavaScript RegExp to create regular expression and array.filter(function) to remove elements that does not match the regex.您可以使用JavaScript RegExp创建正则表达式,使用array.filter(function)删除与正则表达式不匹配的元素。

Sample Code:示例代码:

function cleanFullMatch(){ 
  // Your main array  
  var originalArray = ['www.google.com/rugby', 'www.yelp.com/abc', 'www.yahoo.com/tennis', 'www.bing.com/xyz', 'www.google.com/football']; 
  Logger.log(originalArray);
  
  // This array contains strings that needs to be removed from main array 
  var itemsToBeRemoved = ['google.com', 'yahoo.com', 'google.com'];
  
  // loop through itemsToBeRemove
  itemsToBeRemoved.forEach(function(rgx){
    
    // convert each element of itemsToBeRemove to regular expression
    var re = new RegExp(rgx);
    
    // filter the array by removing the elements that match the regex
    // test() method checks if the string matches the regex
    originalArray = originalArray.filter( word => !re.test(word));
  });
  
  Logger.log(originalArray);
}

Sample Output:示例输出:

在此处输入图片说明

filter out the elements of the originalArray so that none of the itemsToBeRemoved is present in any of the filtered elements. 过滤originalArray的元素,以便任何itemsToBeRemoved都不存在于任何过滤的元素中。

There are many ways to check whether a substring is included in another string, as others said, like includes , indexOf and RegExp .有很多方法可以检查一个子字符串是否包含在另一个字符串中,就像其他人所说的那样,例如includesindexOfRegExp

You could then iterate through all of these itemsToBeRemoved in different ways.然后,您可以以不同的方式遍历所有这些itemsToBeRemoved You could, for example, use some :例如,您可以使用some

function cleanFullMatch(){ 
  var originalArray = ['www.google.com/rugby', 'www.yelp.com/abc', 'www.yahoo.com/tennis', 'www.bing.com/xyz', 'www.google.com/football']; 
  var itemsToBeRemoved = ['google.com', 'yahoo.com'];
  var cleanArray = originalArray.filter(element => !itemsToBeRemoved.some(item => element.includes(item)));
  console.log(cleanArray);
}

Or, alternatively, every :或者,或者, 每个

var cleanArray = originalArray.filter(element => itemsToBeRemoved.every(item => !element.includes(item)));

As you can see, the filtering process can be reduced to a single line.如您所见,过滤过程可以减少到一行。

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

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