Associative array in JavaScript

We can use associative arrays in javascript somewhat similar to PHP. Here is an example:

  1.  
  2. /* Declaring array in javascript */
  3. var opensource = new Array();
  4.  
  5. /* assigning values to this javascript associative array */
  6.  
  7. opensource[‘cms’] = ‘Joomla’;
  8. opensource[‘forum’] = ‘phpBB’;
  9. opensource[‘blog’] = ‘Wordpress’;
  10. opensource[‘os’] = ‘Linux’;
  11. opensource[‘database’] = ‘MySQL’;
  12. opensource[‘webserver’] = ‘Apache’;
  13.  

We can navigate this array as

  1.  
  2. for(script in opensource)
  3. {
  4. //to get the key for each index
  5. alert(script)
  6. //to get the value for each position
  7. alert(opensource[script]);
  8. }
delicious | digg | reddit | facebook | technorati | stumbleupon | chatintamil
Report This Post

No comments yet.

Write a comment: