检查数组的字符串是否显示为另一个数组的字符串

时间:2016-06-29 04:51:55

标签: javascript arrays string data-structures string-comparison

我有一个像这样的字符串数组:

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"
];

...我想要做的是检查inputArray中的每个字符串,看它们是否包含names数组中的任何名称。

每当找到名称匹配时,它应该做两件事:

  1. 将名称推送到answerKey数组,如下所示:

    var answerKey = [ “鲍勃”, “提摩太前书”, “珍妮” “提摩太前书”, “杰里米” ];

  2. 和2.推送名称替换为'?'的字符串到另一个数组(输出)如下:

    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"
    ];
    

    我熟悉检查字符串中的子字符串,但是当子字符串在一个数组中并且要检查的字符串在另一个数组中时,我不熟悉。任何帮助将非常感激:))

4 个答案:

答案 0 :(得分:1)

检查是否有效:

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

此处的预期输出数组。

答案 1 :(得分:1)

使用array.prototype.maparray.prototype.filter获取帮助:

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

顺便说一下,名字数组是小写的,只需使用toLowerCase

JSFIDDLE

答案 2 :(得分:0)

在这种情况下你需要做的是使用嵌套for循环然后检查子串。

<?php
$array = 
    Array ( 0 => Array ( 'id' => '79','firstname' => 'John','lastname' => 'Doe','province' => 'Province','email' => 'someemail@gmail.com','primaryphone' => '123-456-7890',), 1 => Array ( 'id' => '113','firstname' => 'Jane','lastname' => 'Doe','province' => 'Province','email' => 'email@gmail.com','primaryphone' => '123-456-7890',) )
;
$array2 = 
     Array ( 0 => Array ( 'userid' => '79'), 1 => Array ( 'userid' => '115'), 2 => Array ( 'userid' => '124') );

foreach($array as $key => $userInfo) {
    $isMember = 0;
    foreach($array2 as $user) {
        if($userInfo['id'] == $user['userid']) {
            $isMember = 1;
            break;
        }
    }
    $array[$key]['ismember'] = $isMember;
}

答案 3 :(得分: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);