0
 var markers = [
        {
          coords:{lat:  14.5858544, lng: 120.9763816},
        }
      ];

Hi everyone, how do I output the lat and lng? I can't seem to access them. I have tried

document.getElementById("demo").innerHTML = markers[0].coords

document.getElementById("demo").innerHTML =  markers['coords'][0]['lat'];

but it doesn't work

Kyl
  • 17
  • 6

8 Answers8

2

markers[0].coords.lat

or

markers[0]['coords']['lat']

Full documentation about this subject: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

Lee Kowalkowski
  • 11,591
  • 3
  • 40
  • 46
1

The way you access different types is wrong. Try this:

var markers = [
  {
    coords: {
      lat:  14.5858544,
      lng: 120.9763816
    },
  }
];

// To get lat
console.log(markers[0].coords.lat) // 14.5858544
// To get lng
console.log(markers[0].coords.lat) // 120.9763816
baeyun
  • 228
  • 1
  • 6
1

 var markers = [
        {
          coords:{lat:  14.5858544, lng: 120.9763816},
        }
      ];
      
function a(){
document.getElementById("demo_lat").innerHTML = markers[0].coords.lat;
document.getElementById("demo_lng").innerHTML = markers[0].coords.lng;
}
<p id="demo_lat"></p>
<p id="demo_lng"></p>
<button onclick="a()">Show</button>
yajiv
  • 2,901
  • 2
  • 15
  • 25
1

For your HTML

 var markers = [{
     coords: {
         lat: 14.5858544,
         lng: 120.9763816
     },
 }];

You can do either:

document.getElementById("lat").innerHTML = markers[0].coords.lat;
document.getElementById("lng").innerHTML = markers[0].coords.lng;

or

document.getElementById("lat").innerHTML = markers[0]['coords']['lat'];
document.getElementById("lng").innerHTML = markers[0]['coords']['lng'];

To print them on console

 console.log(markers[0].coords.lat)
 console.log(markers[0].coords.lng)
myselfmiqdad
  • 2,518
  • 2
  • 18
  • 33
0

You can get the lat and lng like this:

var markers = [
        {
          coords:{lat:  14.5858544, lng: 120.9763816},
        }
      ];
      console.log( markers[0].coords.lat)
       console.log( markers[0].coords.lng)
Scath
  • 3,777
  • 10
  • 29
  • 40
0

var markers = [
    {
        coords: {lat:  14.5858544, lng: 120.9763816},
    }
];

document.getElementById(`demo`).innerHTML = `lat: ${markers[0].coords.lat}<br>lng: ${markers[0].coords.lng}`;
<div id="demo"></div>
rafaelgomesxyz
  • 1,405
  • 8
  • 14
0
  const getKeyOfObject = (key) => markers.map(item => 
  item.coords[key]).join('')

  getKeyOfObject('lat') // 14.5858544

oldmayn
  • 161
  • 2
  • 3
  • 14
0
  • The first approach only prints the Object coords instead of lat and lng.
  • The second approach is trying to access an attribute called coords which doesn't exist.

With that scenario, you can use Destructuring Assignment:

This snippet loops over the possible markers

var markers = [{
  coords: {
    lat: 14.5858544,
    lng: 120.9763816
  },
}];

markers.forEach(function(marker) {
  var { lat, lng } = marker.coords; //Destructuring Assignment.
  console.log(lat, '-', lng);
});
Ele
  • 33,468
  • 7
  • 37
  • 75