AmyH
AmyH

Reputation: 23

Check if an Array's strings appear as a part of another array's strings

I have an array of strings like so:

var inputArray= [ 
    "Bob Johnson goes to the zoo",
    "Timothy Smith likes to eat ice-cream",
    "Jenny wants to play with her friends",
    "There is no one in the room to play with Timothy",
    "Jeremy Jones has been sleeping all day"
 ];

...and another array of names like so:

var names = [
"bob johnson",
"bob",
"timothy smith",
"timothy",
"jenny sanderson",
"jenny",
"jeremy jones",
"jeremy"
];

...and what I want to do is check each of the strings in the inputArray to see if they contain any of the names from the names array.

Whenever it finds a name match, it should do two things :

  1. Push the name to an answerKey array like so:

    var answerKey = [ "bob", "timothy", "jenny", "timothy", "jeremy" ];

and 2. Push the string with the name replaced by an '?' to another array (output) like so:

var output = [
"? goes to the zoo",
"? likes to eat ice-cream",
"? wants to play with her friends",
"There is no one in the room to play with ?",
"? has been sleeping all day"
];

I'm familiar with checking for substrings within strings but not when substrings are in one array and strings to be checked against are in another. Any help would be very appreciated :))

Upvotes: 0

Views: 79

Answers (4)

Amir Popovich
Amir Popovich

Reputation: 29836

Use array.prototype.map and array.prototype.filter for help:

var inputArray = [
  "Bob Johnson goes to the zoo",
  "Timothy Smith likes to eat ice-cream",
  "Jenny wants to play with her friends",
  "There is no one in the room to play with Timothy",
  "Jeremy Jones has been sleeping all day"
];

var names = [
  "Bob Johnson",
  "Bob",
  "Timothy Smith",
  "Timothy",
  "Jenny Sanderson",
  "Jenny",
  "Jeremy Jones",
  "Jeremy"
];

var answers = [];
var outputArr = inputArray.map(function(row){
   var matches = names.filter(function(name){ return row.indexOf(name) > -1 });
   matches.forEach(function(match){ answers.push(match); row = row.replace(match, '?')});
   return row;
});

console.log('answers: ' + answers);
console.log('outputArr: ' + outputArr);

BTW, it the names array is in lower case, simply use toLowerCase.

JSFIDDLE.

Upvotes: 1

Aswathy S
Aswathy S

Reputation: 729

var output = new Array();
names.forEach(function(field, index) {

    inputArray.forEach(function(key, val) {
          var str1 = key.toUpperCase();
          var str2 = field.toUpperCase();;
          var stat  = str1.contains(str2);
          if(stat == true){
            newArray.push(str1.replace(str2,"?"));
          }

    });
});
console.log(output);

Upvotes: 0

user6360214
user6360214

Reputation:

Check if this works:

var output = [];
for(var c in inputArray){
  output[c] = inputArray[c].toLowerCase();
  for(var o in names){
    output[c] = output[c].replace(names[o],"?");
  }
}

The expected output array right here.

Upvotes: 1

alecschrader
alecschrader

Reputation: 371

What you need to do in this case is use nested for loops then check for the substrings.

var answerKey = [], output = [];
for(var i = 0; i < inputArray.length; i++){
  for(var j = 0, len = names.length; j < len; j++){
    if(inputArray[i].toLowerCase().indexOf(names[j]) > -1){
      answerKey.push(names[j])
      output.push(inputArray[i].toLowerCase().replace(names[j], '?'))
    }
  }
}

Upvotes: 0

Related Questions