Search for value in array with .inarray()

.inarray()


Description: Search within your arrays for specific values.


Example:

<!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>inarray demo</title>
      <script src="http://code.bitsycode.com/bitsycode-latest.min.js"></script>
    </head>
    <body>
        
      <table cellpadding="10" cellspacing="0" class="blocks">
          <tr>
              <td width="150" valign="top"><b>INARRAY:</b></td>
              <td><input type="text" name="value_inarray" class="value_inarray" value="" /></td>
          </tr>
          <tr>
              <td valign="top"><b>TEST ARRAY:</b></td>
              <td>['HTML5', 'CSS3', 'jQuery', 'HTML', 'CSS', 'HTML5', 'HTML5CSSJQUERY']</td>
          </tr>
          <tr>
              <td valign="top"><b>Options:</b></td>
              <td>
                  <div class="form-group" style="margin-bottom:0px">
                      <input type="checkbox" name="any" id="any" value="true" />
                      <label for="any">
                        <span>Any<br />
                        <small>Checks for word anywhere in string <b>(overrides "Exact match")</b></small></span>
                      </label>
                  </div>
                  <div class="form-group">
                      <input type="checkbox" name="exact" id="exact" value="true" /> 
                      <label for="exact">
                        <span>Exact match<br />
                        <small>Check for exact match, using case sensitivity <b>(case sensitive)</b></small></span>
                      </label>
                  </div>
              </td>
          </tr>
          <tr>
              <td></td>
              <td><input type="button" name="submit" onclick="inarray();" value="Validate" /></td>
          </tr>        
          <tr>
              <td valign="top"><b>Results:</b></td>
              <td><span class="results"></span></td>
          </tr>           
      </table>         
      
      <script>
         
      function inarray() {
         
         var exact_checked = jQuery("input[name=exact]").is(":checked");
         var any_checked = jQuery("input[name=any]").is(":checked");
           
         if (bitsyCode(".value_inarray").val().length == 0) {
             
             alert("Please enter a keyword.");
             bitsyCode(".value_inarray").focus();
             
         } else {
             
             var testArray = ['HTML5', 'CSS3', 'jQuery', 'HTML', 'CSS', 'HTML5', 'HTML5CSSJQUERY'];
             var results = bitsyCode.inarray(bitsyCode(".value_inarray").val(), { array:testArray, any:any_checked, exact:exact_checked });                
             
             var matches = "";
             for(var i=0; i < results.matches.length; i++){
                 matches += "<p><b>String: </b>" + results.matches[i].string + "<br />";
                 matches += "<b>Position: </b>" + results.matches[i].position + "</p>";
             }
             
             var response = "<b>Matches found:</b> " + results.found + "<br />";
                 response += "<b>Number of matches found:</b> " + results.count + "<br /><br />";
                 response += "<b>Matches:</b> " + matches;
                 
             bitsyCode(".results").html(response);
             
         }
      }
         
      </script>
     
    </body>
    </html>

Preview:

INARRAY:
TEST ARRAY: ['HTML5', 'CSS3', 'jQuery', 'HTML', 'CSS', 'HTML5', 'HTML5CSSJQUERY']
Options:
Results:
  • Usage

    • How to use:

      Search within an array for values, including in case-sensitive or case-insensitive searches.

    • USAGE:

      bitsyCode.inarray('CSS3', { array:['HTML','CSS3','JavaScript'] });
  • Options

    • What options are there:
      array: [] (default value) // Specify the array you want to search for value in.
      exact: false (default value) // Specify whether you want to search for exact value with case-sensitive or case-insensitive.
      any: false (default value) // Searchs for value anywhere in a string (overrides "Exact match")
  • Returns

    • Returns:

      The return type is an object.

    • Return:
      object { found: true, count: 1, matches: [{ string: "HTML5", position: 1 }] }
    • Return Object breakdown:
      object {
              found: true/false, /* Has any matches been found True(yes), False(no). */
              count: 0, /* How many matches found? */
              matches:
                  Array [
                      object {
                          string: "HTML5", /* String in which match found */
                          position: 1 /* Position string found in array */
                      },
                      object {
                          string: "HTML5", /* String in which match found */
                          position: 6 /* Position string found in array */
                      }
                  ]
      }
Share