#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    33
    Rep Power
    4

    Accessing tuples in an array.


    Im working with an array of latitude/longitude pairs, but I was hoping to just put the tuples in the array and access them one at a time. but if I have code like so:

    var markers = [(36.8653194, -76.05079490000003),(42.06554029999999, -87.98737240000003)]

    alert(markers[0])

    it will print out "-76.05079490000003"

    and alert(markers[1]) would be "-87.98737240000003"

    So obviously its just printing the second half of each of the tuples without letting me access the first half. Im sure Im overlooking something trivial but this doesnt make sense to me. Any tips?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Hi,

    there are no "tuples" in JavaScript. Wherever you got this syntax from, it does not do what you might expect. What you have there is the comma operator, which simply yields the last value in the list. So
    Code:
    (36.8653194, -76.05079490000003)
    is exactly the same as
    Code:
    -76.05079490000003
    I guess what you want is a nested array:
    Code:
    var markers = [[36.8653194, -76.05079490000003],[42.06554029999999, -87.98737240000003]];
  4. #3
  5. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,124
    Rep Power
    4303
    The first ones aren't accessible because of how parentheses and the comma operator work in JavaScript. Parenthesis used like that create a sub-expression and within that the comma operator makes only that last expression within the sub-expression be used as the value for the sub-expression. So when you write this:
    Code:
    var markers = [(36.8653194, -76.05079490000003),(42.06554029999999, -87.98737240000003)]
    the browser sees this:
    Code:
    var markers = [-76.05079490000003, -87.98737240000003];
    You either need to make them be nested arrays:
    Code:
    var markers = [[36.8653194, -76.05079490000003],[42.06554029999999, -87.98737240000003]];
    OR make them be strings:
    Code:
    var markers = ['36.8653194, -76.05079490000003','42.06554029999999, -87.98737240000003'];
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    33
    Rep Power
    4
    Originally Posted by Jacques1
    Hi,

    there are no "tuples" in JavaScript. Wherever you got this syntax from, it does not do what you might expect. What you have there is the comma operator, which simply yields the last value in the list. So
    Code:
    (36.8653194, -76.05079490000003)
    is exactly the same as
    Code:
    -76.05079490000003
    I guess what you want is a nested array:
    Code:
    var markers = [[36.8653194, -76.05079490000003],[42.06554029999999, -87.98737240000003]];
    Ok yah that works, I was hoping tuples existed because I already had the list formatted but this works too. This arises a new question though.

    markers[i] will give me the point 36.8653194, -76.05079490000003

    Im trying to use this with the Google Map API and these points would be used in the line

    var pos = new google.maps.LatLng(markers[i]);
    *which is supposed to take in a num, num parameter*

    but it wont create the point. Im not sure why it wont.
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    The method expects two separate numbers, so that's what you have to pass:
    Code:
    var pos = new google.maps.LatLng(markers[i][0], markers[i][1]);

IMN logo majestic logo threadwatch logo seochat tools logo