skip to Main Content

I created a classical Array

Gpx_Array = new Array;

In another function I dinamically populate my Array

Gpx_Array.push(coords);

Problems starting when Array is back to main function: if I check it with

console.log(Gpx_Array);
[]
0:(588) [M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, …]
1:(819) [M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, …]
length:2
[[Prototype]]:Array(0)

but using

console.log(JSON.stringify(Gpx_Array, null, 2));

I get just [ ],

When I try to access

console.log(Gpx_Array[0]);

I get ‘undefinded’

Why I can’t go inside array, especially as I can see it with

console.log(Gpx_Array);

If I go on console browser, right click on array result and copy object I get exacly what I need (I post only a part)

[
[{"lat":46.204669,"lng":13.347643,"meta":{"ele":"687.5"}},
{"lat":46.204666,"lng":13.347667,"meta":{"ele":"685.2"}},
{"lat":46.20467,"lng":13.347719,"meta":{"ele":"684.5"}},
{"lat":46.204691,"lng":13.347817,"meta":{"ele":"684.1"}},
{"lat":46.204706,"lng":13.347858,"meta":{"ele":"683.9"}},
...],
[
{"lat":46.484188,"lng":12.865198,"meta":{"ele":"527.8"}},
{"lat":46.484205,"lng":12.865215,"meta":{"ele":"528.2"}},
{"lat":46.484231,"lng":12.865284,"meta":{"ele":"528.3"}},
{"lat":46.48424,"lng":12.865375,"meta":{"ele":"528.3"}},
{"lat":46.484217,"lng":12.865452,"meta":{"ele":"528.5"}},
...
]
]

I tried even strange solutions like

Gpx_Array[], Gpx_Array[][0], Gpx_Array[""]… Without any benefit.

Any suggestion is wellcome, thanks in advance

2

Answers


  1. You have to use

    Gpx_Array = new Array();
    

    As it is a class and it needs to be instantiated before it can be used.
    You can also use:

    Gpx_Array = [];
    

    Which automatically creates a new Array object

    Login or Signup to reply.
  2. If I implement the code based on your instruction, then it behaves properly in my case. Therefore the issue is something related with your implementation that you do not share with us. See:

    function first() {
        let Gpx_Array = new Array;
        second(Gpx_Array);
        console.log(Gpx_Array);
    }
    
    function second(Gpx_Array) {
        for (let coords = 1; coords < 4; coords++) Gpx_Array.push({lat: coords, long: coords});
    }
    
    first();
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search