如何在一个数组中查找多个字符串项,并删除匹配搜索和return数组计数的字符串?

How to find multiple string items in an array and delete the string that matches the search and return the count of the array?

var stringsToSearch = ["cat", "dog", "rat"];
fromThisArray = ["rabbit", "snake", "cow", "cat", "dog"];

如果在 fromThisArray 中找到 stringToSearch,它应该删除那些字符串和 return 数组的计数。

此论坛中提出了类似的问题,但他们找到了单个字符串,但我的要求是搜索多个字符串并删除这些字符串。

var stringsToSearch = ["cat", "dog", "rat"];
var fromThisArray = ["rabbit", "snake", "cow", "cat", "dog"];

for(var i=0; i<stringsToSearch.length; i++){
    var idx = fromThisArray.indexOf(stringsToSearch[i]);
    if(idx > -1){
        fromThisArray.splice(idx, 1);
    }   
}

console.log(fromThisArray, fromThisArray.length);

通过使用 filterindexOf

var stringsToSearch = ["cat", "dog", "rat"];

var fromThisArray = ["rabbit", "snake", "cow", "cat", "dog"];

var filteredArray = fromThisArray.filter(function(item){
  return stringsToSearch.indexOf(item) < 0;
});

console.log(filteredArray);

function uniqueCount(arr, stringsToSearch ){

  for(var i = 0; i < stringsToSearch.length ; i++){
    while(arr.indexOf(stringsToSearch[i])> -1){
       arr.splice(i,1);//remove
    }
  }
  return arr.length;
}

var stringsToSearch = ["cat", "dog", "rat"];
var fromThisArray = ["rabbit", "snake", "cow", "cat", "dog"];

//call the function 
console.log(uniqueCount(fromThisArray , stringsToSearch ));