0

Look at this code:

 //This "loop for" converts the GPS latitude and longitude format to Google maps format.
for (var counter1 = 0; counter1 < arrayLatitude.length; counter1++) {
    //Convert ddmm.mmmm to dd.dddd latitude
    var latitude1 = arrayLatitude[counter1].substr(0, 2);
    var latitude2 = arrayLatitude[counter1].substr(2, 9);
    var latitude3 = latitude2 / 60;
    arrayLatitude[counter1] = parseFloat(latitude1) + parseFloat(latitude3);


    //Convert ddmm.mmmm to dd.dddd longitude
    var longitude1 = arrayLongitude[counter1].substr(0, 2);
    var longitude2 = arrayLongitude[counter1].substr(2, 9);
    var longitude3 = longitude2 / 60;
    arrayLongitude[counter1] = parseFloat(longitude1) + parseFloat(longitude3);
}

Here I convert the GPS module coordinates to Google maps coordinates and save them to the array. Every things work good but the first cell of array arrayLatitude[0] and array arrayLongitude[0] have NaN value. This drives me crazy. Please help.

My problem is:

arrayLongitude[0] = NaN;
arrayLongitude[0] = NaN;

but other cells have the right value.

Edit: And this is the simples form of my problem:

alert(arrayLatitude[0]); //3241.1234

//This "loop for" converts the GPS latitude and longitude format to Google maps format.
for (var counter1 = 0; counter1 < arrayLatitude.length; counter1++) {
    //Convert ddmm.mmmm to dd.dddd latitude
    var latitude1 = arrayLatitude[counter1].substr(0, 2);
    var latitude2 = arrayLatitude[counter1].substr(2, 9);
    var latitude3 = latitude2 / 60;

    alert(arrayLatitude[0]); //3241.1234

    arrayLatitude[counter1] = parseFloat(latitude1) + parseFloat(latitude3);

    alert(arrayLatitude[0]); //NaN
}

I have an array that the first cell of it is damaged so I need to put a null character in the first cell and then start the for loop from 1 instead of 0 or remove the first cell of array like this:

arrayLatitude.splice(0, 1);
arrayLongitude.splice(0, 1);

or using:

arrayLatitude.shift();

In this example I saved "" character to the first cell of array and then remove it.

Brad Larson
  • 169,393
  • 45
  • 393
  • 567
Parsa Soroori
  • 63
  • 3
  • 11
  • 1
    if you want to skip/remove them, just `arrayLongitude.shift();arrayLatitude.shift();` before/after the loop – Crayon Violent Mar 02 '14 at 06:16
  • 2
    What have you done to troubleshoot this issue? There's obviously nothing special about the first member of an array. – cookie monster Mar 02 '14 at 06:16
  • Start the offset with 1 count. Shouldn't that correct the issue? – Casey ScriptFu Pharr Mar 02 '14 at 06:17
  • could you show what `alert(arrayLatitude[0])` shows? @CrayonViolent: he could just start the loop from 1 ;) – naveen Mar 02 '14 at 06:18
  • cookie monster@ I don't know. This array contains a string from before. – Parsa Soroori Mar 02 '14 at 06:18
  • naveen@ alert(arrayLatitude[0]) shows NaN. – Parsa Soroori Mar 02 '14 at 06:20
  • 1
    @naveen sure, that's an alternative. As for me, when I'm looping through an array, I'd rather have it be "cleaned up" first. But that's just IMO. – Crayon Violent Mar 02 '14 at 06:20
  • I can't remove the first cell of array. I need it. – Parsa Soroori Mar 02 '14 at 06:20
  • 1
    @ParsaSoroori so it sounds like you want to skip it in this conversion, and (for some reason) you wish the first element to not be like the other elements. If that's your jam then as @naveen said, just start your loop at `1` instead of `0` – Crayon Violent Mar 02 '14 at 06:22
  • @ParsaSoroori: tell us whats inside it then. CrayonViolent's thought is right. We both see the first cell as invalid data. Maybe you put headers there? – naveen Mar 02 '14 at 06:23
  • naveen@ I put it in the second part. – Parsa Soroori Mar 02 '14 at 06:26
  • My problem is here: arrayLatitude[counter1] = parseFloat(latitude1) + parseFloat(latitude3); – Parsa Soroori Mar 02 '14 at 06:27
  • 1
    @ParsaSoroori but what is element 0 supposed to be (before the loop)? I'm having trouble understanding why element 0 should remain, but be excluded (which you've already gotten an answer for how: start at 1 instead of 0 in your loop). *Usually* each element of an array is supposed to have similar data, so that you can act on it in a similar fashion and not have to worry about making exceptions. So, either element 0 is "bad data" and should be removed, or it's "good data" that *probably* shouldn't even be stored in the array. But in any case, *you already got an answer: start at 1 instead of 0* – Crayon Violent Mar 02 '14 at 06:28
  • 1
    I'm getting the impression you don't understand how to "start at 1 instead of 0". `for(var counter1 = 1; counter1 < arrayLatitude.length; counter1++)`.. see? now element 0 is skipped. – Crayon Violent Mar 02 '14 at 06:32
  • @Crayon Violent: Thanks I will do it. Would you please help me to use the zero cell. I need it. I don't want to skip it. Can I save it in the last cell? This was my method before I ask for help: arrayLatitude.splice(0, 1); arrayLongitude.splice(0, 1); – Parsa Soroori Mar 02 '14 at 06:37
  • My problem is not solved because I think the string in cell number 0 is damaged: arrayLatitude = arrayLatLonComma[0].split(",");arrayLatLonComma[0].split(","); – Parsa Soroori Mar 02 '14 at 07:17
  • I used split to save for example 3212.1234,3211.1256,3222.2435 to an array. I think split function has a problem. – Parsa Soroori Mar 02 '14 at 07:21
  • Possible duplicate of [How to find first element of array matching a boolean condition in JavaScript?](http://stackoverflow.com/questions/10457264/how-to-find-first-element-of-array-matching-a-boolean-condition-in-javascript) – Paul Sweatte Feb 26 '16 at 12:37

0 Answers0