Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting JS to use variable contents instead of

    Hi all!

    First post here (obviously)...

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

    Code:
    for(var m=1; m<=86; m++) {
     var currgroup = "group" + m;
     alert(currgroup);
     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,

    Matt
    Last edited by MatthewJT; 11-13-2009 at 10:15 PM. Reason: Title should be *instead of variable name

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,535
    Thanks
    3
    Thanked 513 Times in 500 Posts
    var currgroup = Window["Group"+m];
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Or
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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,

    Matt

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Yes? So why don't you use an array, instead of individual variables???

    That is, instead of doing
    Code:
    var Group17 = ....
    you do something like:
    Code:
    var Group = [];
    ...
    Group[17] = ....
    And then you'll be able to simply do
    Code:
    var currgroup = Group[m];
    and so forth.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Okay, but I don't see why it would be a big deal to change
    Code:
    var group1 = ...
    to
    Code:
    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?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •