gpt4 book ai didi

javascript - 尝试使用 $.each jquery 迭代器。这两者有什么区别?

转载 作者:行者123 更新时间:2023-11-28 11:40:10 27 4
gpt4 key购买 nike

vertices 是 google.maps.LatLng 对象的数组,因此它们应该返回 latlng 点。它对于第一个代码片段来说效果很好。我在使用第二个时遇到问题。

// Iterate over the vertices.
for (var index =0; index < vertices.length; index++) {
var value = vertices.getAt(index);
contentString += "<br />" + "Coordinate: " + index + "<br />" + value.lat() +"," + value.lng();
}

我觉得这段代码应该意味着完全相同的事情,但是当我使用每个迭代器时,我收到一个 javascript 错误:Uncaught TypeError: Cannot call method 'lat' of undefined

  $.each(vertices, function(index, value){
contentString += "<br />" + "Coordinate: " + index + "<br />" + value.lat() +"," + value.lng();
});

最佳答案

它会抛出错误,因为 vertices 不是普通的 javascript 数组(Javascript 中的 Array 对象没有 getAt 方法)。根据 Google 文档:

A polyline specifies a series of coordinates as an array of LatLng objects. To retrieve these coordinates, call the Polyline's getPath(), which will return an array of type MVCArray. Note: you cannot simply retrieve the ith element of an array by using the syntax mvcArray[i]; you must use mvcArray.getAt(i).

所以你应该仍然有:

$.each(vertices, function(i) { var value = vertices.getAt(i); ... });

关于javascript - 尝试使用 $.each jquery 迭代器。这两者有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7628623/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com