View Full Version : Getting JS to use variable contents instead of

11-13-2009, 10:02 PM
Hi all!

First post here (obviously)...

I seem to have an issue with getting javascript to recognize the value of a variable.

for(var m=1; m<=86; m++) {
var currgroup = "group" + m;
if(currgroup.Contains(point)) {
var userNG = m;
var foundNG = true
} else {
var foundNG = false;

If you see where I alert the value of currgroup, it displays "Group1" as it should... so the next line should be Group1.Contains(point); but it errors out as "currgroup.Contains(point) is not a function".

If however I hard code it with Group1.Contains(point) it works fine.

Is there a way to tell JS explicitly that I want it to use the variable contents instead of the name that I'm missing?

Thanks a bunch for any and all help,


11-13-2009, 11:18 PM
var currgroup = Window["Group"+m];

Old Pedant
11-13-2009, 11:46 PM

var currgroup = eval("Group" + mx);

But I'd say more than likely there is something wrong with your overall design if you really have JS variables named Group1, Group2, Group3, ..., Group86.

You should *SURELY* have an *array* instead.

11-14-2009, 10:05 AM
Thanks for the response. To give a little background, it is a google map application and the groups are actually neighborhood group polygons representing their boundaries.

Thanks again,


Old Pedant
11-15-2009, 12:23 AM
Yes? So why don't you use an array, instead of individual variables???

That is, instead of doing
var Group17 = ....you do something like:

var Group = [];
Group[17] = ....

And then you'll be able to simply do

var currgroup = Group[m];
and so forth.

11-16-2009, 01:34 AM
I understand your point, but I don't think you're understanding the application. Just because I'm new to JS does not mean I'm new to coding.

Each group (1-80+) are polygons i.e.

var group1 = new GPolygon([
new GLatLng(41.6038893886,-90.5392170403),
new GLatLng(41.6039375489,-90.5295636659),
new GLatLng(41.6039850181,-90.51987446),
new GLatLng(41.6040686776,-90.512222768),

It is a finite number of groups that will never change. Not much use in spending the extra time to wrap it all in an array, but to each his own I guess.

Thanks again for the help

Old Pedant
11-16-2009, 08:03 PM
Okay, but I don't see why it would be a big deal to change

var group1 = ...

var groups = []; // one time, of course
groups[1] = ...

But never mind. For what you are doing, almost sure doing eval() or Window[] will be adequate. Surely not a performance issue here, right?