简体   繁体   English

如何使用 $.each 遍历数组?

[英]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.因为我要在我的地图中使用这个值作为 latLng 来显示一个图标。所以我试过这个。

 $.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?我怎样才能分别设置每组 LatLng?

Study the structure of latLng .研究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.不过,在继续之前,您可能想重新考虑latLng的结构。

you must use the following:您必须使用以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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