简体   繁体   中英

How to loop through array using $.each?

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

在此处输入图片说明

How can I can each set of LatLng separately?

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.

you must use the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM