Associative array in Javascript

Jun 14th, 2008
Javascript do not have associative array like PHP. In PHP you may have enjoyed associative array.
Here is something similar to associative array in JavaScript. But as it is not an associative array, so you cannot use JavaScript array methods. It is JSON style Object representation.
<script type="text/javascript">

var salesPerson = { "firstName": "crockford",
     "lastName": "Javascript Object Notation(JSON)",
     "address": "Bangalore",
     "phoneNumbers": [
         "22222222",
         "9000000000" ]
 };

key = '';
value1 = '';
for (i in salesPerson)
{
 key += i + ' ';
 value1 += i + ': ' +  eval ( '(' + 'salesPerson.' + i + ')') + "\n";
}

alert(key);
alert(value1);

alert(salesPerson.address);
alert(salesPerson.phoneNumbers);
</script>

Next:

Share/Bookmark

 

Possibly Related posts:

blog comments powered by Disqus