0

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

1
  • Seems like a home work! Commented Jun 29, 2016 at 7:55

4 Answers 4

1

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.

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

Comments

1

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.

Comments

0

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], '?'))
    }
  }
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.