Wednesday, January 11, 2012

in JavaScript a hash is an object not an array and it doesn't carry a length property

I erroneously suggest here that hashes are arrays and that is NOT true. I learned that the hard way today. I was trying to sniff the contents of a dropdown list into a hash and then confirm that my code was working by trying to throw an alert onto the screen which would give the length of the "array" I was populating. I was expecting a result greater than zero, and when I got zero over and over again, no matter what I tried, I assumed that the rest of my code was messed up. However, the reality is that the alert below should return a zero and it is NOT because the hash is empty.

<html>

   <head>

      <script type="text/javascript" src="jquery-1.5.js">

      </script>

   </head>

   <body>

      Which Republican can still beat Romney?<br />

      <select id="dropdown">

         <option value="Egomaniac">Gingrich</option>

         <option value="Stoic">Huntsman</option>

         <option value="Ideologue">Paul</option>

         <option value="Redneck">Perry</option>

         <option value="Nazi">Santorum</option>

      </select>

      <script type="text/javascript">

         
var collection = new Array();

         $("#dropdown option").each(function () {

            collection[$(this).val()] = $(this).text();

         });

         alert(collection.length);


      </script>

   </body>

</html>

 
 

I got the idea for the very first line of my JavaScript, both above and below, from here, but I eventually realized that a hash is not really an array. Rather, you may sort of fudge a hash as an array and you may manually set the length parameter like so:

<html>

   <head>

      <script type="text/javascript" src="jquery-1.5.js">

      </script>

   </head>

   <body>

      Which Republican can still beat Romney?<br />

      <select id="dropdown">

         <option value="Egomaniac">Gingrich</option>

         <option value="Stoic">Huntsman</option>

         <option value="Ideologue">Paul</option>

         <option value="Redneck">Perry</option>

         <option value="Nazi">Santorum</option>

      </select>

      <script type="text/javascript">

         
var collection = new Array();

         collection.length = 0;

         $("#dropdown option").each(function () {

            collection[$(this).val()] = $(this).text();

            collection.length++;

         });

         alert(collection.length);


      </script>

   </body>

</html>

 
 

I hope you like how I am always correcting myself at my blog. Hey, I'm learning. Anyways, the author of this seems to be using array to be able to use length. I looked at other postings online and concluded that:

  1. a hash is an object and not an array in spite of how it may be manhandled
  2. there isn't an easy way to get a length of a hash
  3. if testing, fish out actual data like so:

    <html>

       <head>

          <script type="text/javascript" src="jquery-1.5.js">

          </script>

       </head>

       <body>

          Which Republican can still beat Romney?<br />

          <select id="dropdown">

             <option value="Egomaniac">Gingrich</option>

             <option value="Stoic">Huntsman</option>

             <option value="Ideologue">Paul</option>

             <option value="Redneck">Perry</option>

             <option value="Nazi">Santorum</option>

          </select>

          <script type="text/javascript">

             
    var collection = new Object();

             $("#dropdown option").each(function () {

                collection[$(this).val()] = $(this).text();

             });

             alert(collection['Ideologue']);


          </script>

       </body>

    </html>

No comments:

Post a Comment