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

5
  • post your attempts Commented Dec 30, 2016 at 6:21
  • There are lot of questions of similar type on SO. Just do some research work Commented Dec 30, 2016 at 6:22
  • Please format your code and add more description to the question. Commented Dec 30, 2016 at 6:22
  • Possible duplicate of Javascript array search and remove string? Commented Dec 30, 2016 at 6:24
  • No Efforts posted duplicate. question available on SO. stackoverflow.com/questions/16312528/… Commented Dec 30, 2016 at 6:28

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you every one for your time on this and all answers were close to my requirements.
@skellysanthu Glad, this helped you. Happy coding
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);

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.