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

If stringToSearch is found in fromThisArray, It should delete those strings and return the count of the array.

Similar question were asked in this forum but they are finding single string but my requirement was to search multiple string and delete those string.

santhosh
  • 463
  • 4
  • 15

3 Answers3

0
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);
Aravinder
  • 503
  • 4
  • 8
0

By using filter and indexOf

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);
Jyothi Babu Araja
  • 10,076
  • 3
  • 31
  • 38
0
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 ));
Azad
  • 5,144
  • 4
  • 28
  • 56