简体   繁体   中英

Compare two arrays and remove items with partial match

I need to compare two arrays for PARTIAL / SUBSTRING matches. 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,

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.

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.

There are many ways to check whether a substring is included in another string, as others said, like includes , indexOf and RegExp .

You could then iterate through all of these itemsToBeRemoved in different ways. You could, for example, use 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.

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