1

I am trying to have sql's like clause like effect in javascript.

I am aware that similar question are already present on internet but the following approach is not working for me:

$(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>

I have set substring which is to be looked up in string as "lan", which should push "PLANNING & PROJE         00000000PG" into the array. But it does not.

3
  • 3
    case sensitive?try changing to lowercase first before compare? Commented Nov 14, 2016 at 11:50
  • 1
    @guradio Yep. Commented Nov 14, 2016 at 11:51
  • toLowerCase() to make it case insensitive or you can use localeCompare() to compare the string Commented Nov 14, 2016 at 11:59

3 Answers 3

2

I think that the problem is with the letter casing. Try this :

$(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>

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

1 Comment

Damn it, I know. All we need to take care is to convert either into lowercase or uppercase for comparison.
1

I suggest to search for lower case letters and move the search string outside of the loop.

$(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);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

For a more concise version, you could use Array#filter

$(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);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Comments

0

If you want to push original string then compare with string converted by using toLowerCase() and push the original string as shown below :

 $(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].toLowerCase();

     var substring = "lan";
     if (string.indexOf(substring) !== -1) // compare with string converted by toLowerCase() 
       {

       lstfiltered.push(listAll[i]); // Push original string.

     }
   }

   console.log(lstfiltered);


 });

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.