Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Updated</strong></p> <p>To search in the array with Vanilla js I would use the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter" rel="nofollow noreferrer"><code>filter()</code></a> method implemented into the Array prototype</p> <h3>Hacker style</h3> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>let a = ["foo","fool","cool","god"]; let b = a.filter(item =&gt; item.indexOf('oo') &gt; -1); console.log(b); // ["foo","fool","cool"]</code></pre> </div> </div> </p> <h3>ES6 style (ES2015)</h3> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange']; const filterItems = (needle, heystack) =&gt; { let query = needle.toLowerCase(); return heystack.filter(item =&gt; item.toLowerCase().indexOf(needle) &gt;= 0); } console.log(filterItems('ap', fruits)); // ['apple', 'grapes'] console.log(filterItems('ang', fruits)); // ['mango', 'orange']</code></pre> </div> </div> </p> <h3>ES5 style</h3> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var fruits = ['apple', 'banana', 'grapes', 'mango', 'orange']; function filterItems(needle, heystack) { var query = needle.toLowerCase(); return heystack.filter(function(item) { return item.toLowerCase().indexOf(query) &gt;= 0; }) } console.log(filterItems('ap', fruits)); // ['apple', 'grapes'] console.log(filterItems('ang', fruits)); // ['mango', 'orange']</code></pre> </div> </div> </p> <hr> <p><em>This is the outdated answer</em></p> <p>To search in the array with jQuery you might use <code>jQuery.grep()</code> or <code>jQuery.map()</code>. Both return new array with filtered elements using a callback function.</p> <p>The fastest implementation (case insensitive) is using <code>indexOf</code> and <code>toUpperCase</code> in the callback:</p> <pre><code>var search_term = 'oo'; // your search term as string var search = search_term.toUpperCase(); var array = jQuery.grep(a, function(value) { return value.toUpperCase().indexOf(search) &gt;= 0; }); </code></pre> <p><em>If you don't need case insensitive search you can remove both <code>.toUpperCase()</code> to speed it up even further.</em></p> <p>More flexible but much slower (good enough for small arrays) is to use regular expression:</p> <pre><code>var search_term = "oo"; // search term var search = new RegExp(search_term , "i"); var arr = jQuery.grep(a, function (value) { return search.test(value); }); </code></pre> <p>or</p> <pre><code>var search_term = "oo"; // search term var search = new RegExp(search_term , "i"); var arr = jQuery.map(a, function (value) { return value.match(search) ? value : null; }); </code></pre> <p><em>Regular expressions allow you to make searches much more complex than <code>%value%</code>. However don't use it if you don't need it because it is many times slower.</em></p> <p>you should get an array <code>arr</code> with the matched elements</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload