title: how to prevent adding duplicate keys to a javascript array
body: <p>I found a lot of related questions with answers talking about for...in loops and using hasOwnProperty but nothing I do works properly. All I want to do is check whether or not a key exists in an array and if not, add it.</p>
  
  <p>I start with an empty array then add keys as the page is scrubbed with jQuery.</p>
  
  <p>Initially, I hoped that something simple like the following would work: (using generic names)</p>
  
  <pre><code>if (!array[key])
     array[key] = value;
  </code></pre>
  
  <p>No go. Followed it up with:</p>
  
  <pre><code>for (var in array) {
     if (!array.hasOwnProperty(var))
        array[key] = value;
  }
  </code></pre>
  
  <p>Also tried:</p>
  
  <pre><code>if (array.hasOwnProperty(key) == false)
     array[key] = value;
  </code></pre>
  
  <p>None of this has worked. Either nothing is pushed to the array or what I try is no better than simply declaring <code>array[key] = value</code> Why is something so simple so difficult to do. Any ideas to make this work?</p>