I am a newbie in JS. Here is my code and I believe it should work… but it doesn’t.
var pop = new Array();
pop['la'] = new Array('nt','gb','te');
pop['sa'] = new Array('nt','gb');
pop['ha'] = new Array('pc','pa');
var _ecpop="la";
for (var i = 0; i < pop[_ecpop].length; i++)
{
document.write(pop[_ecpop][i]);
}
I just do not know any alternate way to have a map of vectors of a string.
Thanks,
Amir.
That’s not an Array, but a Javascript Object, containing Arrays in it’s properties. You can use Object and Array literals for that. The advantage is that your code looks much cleaner. There are seldom reasons to use
new Arrayornew Objectin javascript code (see for example this SO Question).now you can use
if a property label is stored in a variable (like you _ecpop), you can use bracket notiation to retrieve it’s value:
The other way around you can assign a label to an Object:
document.writeis not the preferred way to put things on your page. It’s better and just as easy to use some element with an id, and write output to it usinginnerHTML, for example