无法找到字符串是否包含子字符串?

时间:2016-11-14 11:48:26

标签: javascript jquery arrays

我试图在javascript中使用类似sql的效果子句。

我知道类似的问题已经存在于互联网上,但以下方法对我不起作用:

$(document).ready(function() {
  var listAll = [
    "X1",
    "ANTENNA SYSTEMS          00000000AS",
    "Security & Wrokf         00000000CS",
    "MICROWAVE & COMM         00000000MC",
    "MICROWAVE SENSOR         00000000MT",
    "PLANNING & PROJE         00000000PG",
    "MECHANICAL SYSTE         00000000MS",
    "ELECTRO-OPTICAL          00000000EO",
    "SATCOM EXPERIMEN         00000000SE",
    "QUALITY ASSURANC         00000000QA",
    "QUALITY ASSURANC         00000000QC",
    "DATA PRODUCTS SO         00000000DP"
  ];
  var lstfiltered = ["X2"];

  for (i = 0; i <= listAll.length - 1; i++) {
    console.log(listAll[i]);
    var string = listAll[i];
    var substring = "lan";
    if (string.indexOf(substring) !== -1) {
      lstfiltered.push(string);
    }
  }

  console.log(lstfiltered);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

我已将字符串中的子字符串设置为“lan”,这应该将“PLANNING&amp; PROJE 00000000PG”推入数组。但事实并非如此。

3 个答案:

答案 0 :(得分:2)

我认为问题在于字母大小写。试试这个:

$(document).ready(function() {
  var listAll = ["X1", "ANTENNA SYSTEMS          00000000AS", "Security & Wrokf         00000000CS", "MICROWAVE & COMM         00000000MC", "MICROWAVE SENSOR         00000000MT", "PLANNING & PROJE         00000000PG", "MECHANICAL SYSTE         00000000MS", "ELECTRO-OPTICAL          00000000EO", "SATCOM EXPERIMEN         00000000SE", "QUALITY ASSURANC         00000000QA", "QUALITY ASSURANC         00000000QC", "DATA PRODUCTS SO         00000000DP"];
  var lstfiltered = ["X2"];
  
  for (i = 0; i <= listAll.length - 1; i++) {
    console.log(listAll[i]);
    var string = listAll[i];
    var substring = "lan";
    if (string.toLowerCase().indexOf(substring) !== -1) {
      lstfiltered.push(string);
    }
  }

  console.log(lstfiltered);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

答案 1 :(得分:1)

我建议搜索小写字母并将搜索字符串移到循环之外。

&#13;
&#13;
$(document).ready(function() {
  var listAll = ["X1", "ANTENNA SYSTEMS          00000000AS", "Security & Wrokf         00000000CS", "MICROWAVE & COMM         00000000MC", "MICROWAVE SENSOR         00000000MT", "PLANNING & PROJE         00000000PG", "MECHANICAL SYSTE         00000000MS", "ELECTRO-OPTICAL          00000000EO", "SATCOM EXPERIMEN         00000000SE", "QUALITY ASSURANC         00000000QA", "QUALITY ASSURANC         00000000QC", "DATA PRODUCTS SO         00000000DP"];
  var lstfiltered = ["X2"];
  var substring = "lan";
  
  for (i = 0; i <= listAll.length - 1; i++) {
    console.log(listAll[i]);
    var string = listAll[i];
    if (string.toLowerCase().indexOf(substring) !== -1) {
      lstfiltered.push(string);
    }
  }

  console.log(lstfiltered);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

要获得更简洁的版本,您可以使用Array#filter

&#13;
&#13;
$(document).ready(function() {
  var listAll = ["X1", "ANTENNA SYSTEMS          00000000AS", "Security & Wrokf         00000000CS", "MICROWAVE & COMM         00000000MC", "MICROWAVE SENSOR         00000000MT", "PLANNING & PROJE         00000000PG", "MECHANICAL SYSTE         00000000MS", "ELECTRO-OPTICAL          00000000EO", "SATCOM EXPERIMEN         00000000SE", "QUALITY ASSURANC         00000000QA", "QUALITY ASSURANC         00000000QC", "DATA PRODUCTS SO         00000000DP"];
  var substring = "lan";
  var lstfiltered = listAll.filter(function (a) {
          return a.toLowerCase().indexOf(substring) !== -1;
      });

  lstfiltered.unshift('X2')
  console.log(lstfiltered);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

如果你想推送原始字符串,然后与使用toLowerCase()转换的字符串进行比较,并推送原始字符串,如下所示:

Do