简体   繁体   English

检查字符串是否不在数组中 javascript

[英]Check if string not in array in javascript

Yesterday I asked a question and, "with a little help from my friends" got my little javascript working.昨天我问了一个问题,“在我朋友的帮助下”让我的小 javascript 开始工作。

This one:这个:

<script type="text/javascript" >    
function paint_cells() {
  var tds = document.querySelectorAll('tbody td'), i;
  for(i = 0; i < tds.length; i += 3) {
      if(tds[i+1].textContent != tds[i+2].textContent){
      tds[i+2].bgColor = "red";
    }
  }
}
paint_cells();
</script>

Now I would like to tweak this to cater for multiple correct answers.现在我想调整它以满足多个正确答案。 For example, both 'don't hurry' or 'do not hurry' are correct.例如,'don't hurry' 或 'don't hurry' 都是正确的。 In my MySQL table, they are stored as a string like this:在我的 MySQL 表中,它们存储为这样的字符串:

don't hurry|do not hurry别着急|别着急

I want to split the string on |我想在 | 上拆分字符串and then check if the student's answer is in the array (same as I do in PHP when marking the answers).然后检查学生的答案是否在数组中(与我在标记答案时在PHP中所做的相同)。

Not all answers have multiple correct answers.并非所有答案都有多个正确答案。 I hope that is not a problem for.split()??我希望这不是 .split() 的问题?

I tried this, but I am not getting the result I want.我试过了,但没有得到我想要的结果。 Can you please help me tweak this to give me the result I want?你能帮我调整这个给我想要的结果吗?

Edit: I thought maybe the apostrophes were causing the problem, so I escaped them.编辑:我想可能是撇号导致了问题,所以我避开了它们。 I trimmed each variable.我修剪了每个变量。 But I still don't get what I want?但是我还是没有得到我想要的? Any tips please?请问有什么建议吗?

   <script type="text/javascript" >    
function paint_cells() {
  var tds = document.querySelectorAll('tbody td'), i;
  for(i = 0; i < tds.length; i += 3) {
    var arr = tds[i+1].textContent.split('|');
    for(j = 0; j < arr.length; j++) {
        arr[j].trim();
        arr[j].replace(/'/g, "\'");
        }
    var myvar = tds[i+2].textContent;
    myvar.trim();
    myvar.replace(/'/g, "\'");
    //console.log("Your line would be here")//Prints a line on the console 
    if(!arr.includes(myvar)){
        //if(arr[0] != myvar){          
            alert('Array length: ' + arr.length + ' Answers: ' + tds[i+1].textContent + ' Student answer:' + myvar);            
          tds[i+2].bgColor = "red";
    }
  }
}
paint_cells();
</script>

Quotes don't matter.报价并不重要。 You have to reset the array to the trimmed value... trim returns the modified value, it doesn't "change in place".您必须将数组重置为修剪后的值... trim 返回修改后的值,它不会“原地改变”。

var arr = answers.split('|');
for(var ii=0; ii<arr.length; ii++){
    arr[ii] = arr[ii].trim().toLowerCase();
}

Here's a working example (run code snippet below):这是一个工作示例(运行下面的代码片段):

 function paint_cells() { var tds = document.querySelectorAll('tbody td'); for (var i = 0; i < tds.length; i += 3) { var cell_1 = tds[i + 1]; var cell_2 = tds[i + 2]; var answers = cell_1.textContent; var arr = answers.split('|'); for (var ii = 0; ii < arr.length; ii++) { arr[ii] = arr[ii].trim().toLowerCase(); } var guess = (cell_2.textContent || "").trim().toLowerCase(); //console.log("Your line would be here")//Prints a line on the console if (arr.indexOf(guess) < 0) { console.log('Array length: ' + arr.length + ' Answers: ' + answers + ' Student answer:' + cell_2.textContent); cell_2.style.backgroundColor = "red"; } } } paint_cells()
 <table> <tbody> <tr> <td></td> <td>don't hurry|do not hurry</td> <td>do not hurry</td> </tr> <tr> <td></td> <td>don't hurry|do not hurry</td> <td>don't hurry</td> </tr> <tr> <td></td> <td>don't hurry|do not hurry</td> <td>abc</td> </tr> <tr> <td></td> <td>don't hurry|do not hurry</td> <td>xyz</td> </tr> </tbody> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM