-3

I have an array like this

var latLng=[{
                "coordinates":[
                  [
                    [0,0],
                    [0.6,0.6],
                    [3,3],
                    [5.5]
                  ]
                ]
                }];

I need to print each set of value. which means,

[0,0]
[0.6,0.6]
[3,3]
[5.5]

Because am gonna use this values as latLng in my map to show an icon.So I tried this.

 $.each(latLng,function(index,value){
                    console.log(value.coordinates)
                    //L.marker(value.coordinates).addTo(map)
                })

Am getting like this

enter image description here

How can I can each set of LatLng separately?

laz
  • 281
  • 8
  • 18
  • Possible duplicate of [For-each over an array in JavaScript?](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript) – But those new buttons though.. Feb 15 '18 at 07:19
  • @billynoah This has nothing to do with the understanding of a `.each()` loop. It's more about the understanding of a object/array structure. – Ma Kobi Feb 15 '18 at 07:32

2 Answers2

4

Study the structure of latLng. It is an array with one element, which is an object with one field. On this field there is an array containing just another array, which in turn contains the coordinates you're looking for.

So, in order to get the desired result, you'd have to write:

$.each(latLng[0]['coordinates'][0], function(index, value) {
    console.log(value);
});

You'd probably want to rethink the structure of latLng before you continue, though.

Hannes Petri
  • 864
  • 1
  • 7
  • 15
0

you must use the following:

$.each(latLng[0].coordinates[0], function(index,value){
                    console.log(valuе);
                });
VTodorov
  • 953
  • 1
  • 8
  • 25