Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
Can't access JS array from api - undefined

I got geo-coordinates from an API. An try to log them to the console separately. When I log them as an array everything works, but when I try to access the elements in it with array[0] the console says undefined... What am I doing wrong? Here is the code:

  async function get_geocode(x) {
  const response = await fetch(x);
  
  const data = await response.json();
  console.log(data.features[0].center,"center");
  let lo = data.features[0].center[0];
  let la = data.features[0].center[1];
  lo = parseFloat(lo);
  la = parseFloat(la);
  console.log(lo, la, "lola");
  return [lo, la];
}


document.getElementById("mybutton").onclick = function(){
  //let coordinates=[];
  //var coordinates = new Float32Array(2);
  let values = [];
  [...]

  
  values = get_geocode(geocode_api_link);
  console.log(values, "coordinates");
  console.log(values[0], "eins");
  console.log(values[1], "zwei");



}

And the output is:

[Log] (2) (index.js, line 36) Promise

result: [12.345678, 99.345678] (2)

status: "resolved"

Promise Prototyp "coordinates" [Log] undefined – "eins" (index.js, line 37)

[Log] undefined – "zwei" (index.js, line 38)

[Log] (2) (index.js, line 5) [12.345678, 52.456213] (2) "center"

[Log] 12.345678 – 99.345678 – "lola" (index.js, line 10)

So I can log the whole array but not the single elements in it.. What do I do wrong?

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!