Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>jQuery offers <a href="http://api.jquery.com/jQuery.inArray" rel="noreferrer"><code>$.inArray</code></a>:</p> <p>Note that inArray returns the index of the element found, so <code>0</code> indicates the element is the first in the array. <code>-1</code> indicates the element was not found.</p> <p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false"> <div class="snippet-code snippet-currently-hidden"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var categoriesPresent = ['word', 'word', 'specialword', 'word']; var categoriesNotPresent = ['word', 'word', 'word']; var foundPresent = $.inArray('specialword', categoriesPresent) &gt; -1; var foundNotPresent = $.inArray('specialword', categoriesNotPresent) &gt; -1; console.log(foundPresent, foundNotPresent); // true false</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;</code></pre> </div> </div> </p> <hr> <p><em>Edit 3.5 years later</em></p> <p><code>$.inArray</code> is effectively a wrapper for <code>Array.prototype.indexOf</code> in browsers that support it (almost all of them these days), while providing a shim in those that don't. It is essentially equivalent to adding a shim to <code>Array.prototype</code>, which is a more idiomatic/JSish way of doing things. MDN provides <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Polyfill" rel="noreferrer">such code</a>. These days I would take this option, rather than using the jQuery wrapper.</p> <p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false"> <div class="snippet-code snippet-currently-hidden"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var categoriesPresent = ['word', 'word', 'specialword', 'word']; var categoriesNotPresent = ['word', 'word', 'word']; var foundPresent = categoriesPresent.indexOf('specialword') &gt; -1; var foundNotPresent = categoriesNotPresent.indexOf('specialword') &gt; -1; console.log(foundPresent, foundNotPresent); // true false</code></pre> </div> </div> </p> <hr> <p><em>Edit another 3 years later</em></p> <p><sub>Gosh, 6.5 years?!</sub></p> <p>The best option for this in modern Javascript is <code>Array.prototype.includes</code>:</p> <pre><code>var found = categories.includes('specialword'); </code></pre> <p>No comparisons and no confusing <code>-1</code> results. It does what we want: it returns <code>true</code> or <code>false</code>. For older browsers it's polyfillable <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Polyfill" rel="noreferrer">using the code at MDN</a>.</p> <p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false"> <div class="snippet-code snippet-currently-hidden"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var categoriesPresent = ['word', 'word', 'specialword', 'word']; var categoriesNotPresent = ['word', 'word', 'word']; var foundPresent = categoriesPresent.includes('specialword'); var foundNotPresent = categoriesNotPresent.includes('specialword'); console.log(foundPresent, foundNotPresent); // true false</code></pre> </div> </div> </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. 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