In this article about associative array, which looks similar to associative array in PHP. Here is some more ways for simulating associative array like things in Javascript:
<script type="text/javascript">
var obj = new Object();
obj['name'] = 'satya';
obj['sex'] = 'male';
obj['city'] = 'Patna';
obj['age'] = '30';
// this method is not working with Iterator.
obj.length = function () {
ln=0;
for (var pair in obj) {
if (typeof obj[pair] == 'function') continue;
ln++;
}
return ln;
};
alert(obj.length());
/*
These style also works as the above
*/
//var obj = {"name":"satya", "age":30}; // Object
//var obj = ['lang', 'javascript']; // array
for (var i in obj) {
if (typeof obj[i] == 'function') continue;
alert(i + '=' + obj[i]); // name=satya for Obj and 0=lang for array
}
</script>
I have not explained anything in details. You can learn in detail about this associative array here.
Comments (1)