0

I am getting a variable amount of lat and lon returned from my database in the following config

lat,lon*lat,lon*lat,lon ect

So I split on the * which works.

Then I split on the , which seems to work if I print the spicific array cell.

When I try and do the below however it all goes to shit.

When I print

cords[0, 0] + cords[0, 1] + "\r\n" + cords[1, 0] + cords[1, 1] + "\r\n" + cords[2, 0] + cords[2, 1] + "\r\n"

and so on every row has the same exact lat and lon

 41.47033705-81.93612862
 41.47033705-81.93612862
 41.47033705-81.93612862
 41.47033705-81.93612862
...

I debuged this thing all night last night and then a couple hours tonight and I cant seem to find the error.

                var cords = [];
                var response = xmlhttp.responseText;
                var locs = response.split("*");

                for(var len = 0; len < locs.length; len++)
                {
                    var temp = String(locs[len]).split(",");
                    if (temp.length > 1)
                        cords.push(temp[0],temp[1]);

                }

I have also tried cords.push(temp) which doesnt work either

3
  • The variable cords is not part of your code snippet. Where is it declared? Also, String is not meant to be used with new. For string conversion, call String as a function and not as a constructor. Commented Aug 21, 2013 at 1:29
  • jsfiddle.net/Vp7y6 - not reproducible Commented Aug 21, 2013 at 1:30
  • @lightblade cords is declared just above the snip-it and I have tried both with and without new Commented Aug 21, 2013 at 1:30

3 Answers 3

3

If you want to have 2 dimensional array you need to use:

cords.push([temp[0],temp[1]]);

instead and then address it as cords[0][0], etc. In that way you have an array of N elements each of which is an array of exactly 2 elements.

Explanation why your code doesn't work:

cords[2, 1]

expression is treated as access to cords array with 2, 1 index. Whereas 2, 1 is 2 numbers with comma operator. The comma operator returns the last operand passed. So 2, 1 in fact is equal to 1 thus cords[2, 1] goes to cords[1]

Sign up to request clarification or add additional context in comments.

4 Comments

@zerkms you're the man... or woman... what ever you are but you are it... thank you
@AMR: follow the avatar - I'm a cat
@AMR zerkms is not human not animal his his miracle just wait until zerkms is your next president
@zerkms it now works... You have just helped me track people all over the world and plot it on a map.... muahahaha /bow future president...
0
<script>
var locs = "lat1,lon1*lat2,lon2*lat3,lon3";
var arr = locs.split("*");
var cords = new Array();
for (var i=0;i<arr.length;i++){
    var x = arr[i].split(",");
    cords.push(x);
}
//test
for (var i=0;i<cords.length;i++){
    document.write(cords[i][0]+" "+cords[i][1]+"<br/>");
}
</script>

Comments

0

my approach...Each array contains latitude and longitude. http://jsfiddle.net/techsin/HWhMY/

var s = 'lat,lon*lat,lon*lat,lon';
var cords = makeArr(s);

console.log(cords[0][1]);

function makeArr() {
    var g = 0, arr = [],
        n = s.replace(/,|\*/g, ' ').split(' ').map(jn);

    function jn(s) {
        if (g<2) {
            arr.push(s);
            g++;
        } if (g>1) {
           g = 0, r = arr, arr = [];
           return r;
        }

    }
    n.forEach(function (e, i) {if (e == undefined) {n.splice(i, 1);}});
    return n;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.