Error in array, how to format correctly?

#1

Hi,

Please, can someone help me to properly correct the array below?

..............
let c;
answer.position = []
    if (answer.flag === "A") {
        let degreesN = Number(elements[0].substr(13,2))
        let minutesN = Number(elements[0].substr(15,7))
        let ns = elements[0].substr(22,1)                                            // N or S
        let degreesE = Number(elements[0].substr(23,3))
        let minutesE = Number(elements[0].substr(26,7))
        let ew = elements[0].substr(33,1)                                         // E or W
        answer.latitude = degreesN + minutesN/60                         // 2232.9806N
        if (ns === "S") answer.latitude = -answer.latitude
        answer.longitude = degreesE + minutesE/60
        if (ew === "W") answer.longitude = -answer.longitude        //11404.9355E
}
...............................................................

Actually, debug message shows everything at the same level :

position: array[0]
latitude: 22.549676666666667
longitude: 114.082258333333

But I'd like to include latitude and longitude inside the array position, like this :

position: array[1]
     0: object
        latitude: 22.549676666666667
        longitude: 114.08225833333333

I've tried with answer.position.push({ latitude: answer.latitude, longitude: answer.longitude }), but of course, my message is wrong as can read twice the latitude and longitude :

position: array[1]
     0: object
       latitude: 22.549676666666667
       longitude: 114.08225833333333
latitude: 22.549676666666667
longitude: 114.08225833333333

Thanks!

0 Likes

#2
answer.position = [{}]
    if (answer.flag === "A") {
        let degreesN = Number(elements[0].substr(13,2))
        let minutesN = Number(elements[0].substr(15,7))
        let ns = elements[0].substr(22,1)                                            // N or S
        let degreesE = Number(elements[0].substr(23,3))
        let minutesE = Number(elements[0].substr(26,7))
        let ew = elements[0].substr(33,1)                                         // E or W
        answer.position[0].latitude = degreesN + minutesN/60                         // 2232.9806N
        if (ns === "S") answer.latitude = -answer.latitude
        answer.position[0].longitude = degreesE + minutesE/60
        if (ew === "W") answer.longitude = -answer.longitude        //11404.9355E
}

0 Likes

#3

Perfect!!! Thanks Steve-Mcl !! :slight_smile:

0 Likes