Javascript:在循环内生成对象键并将对象数组推送到每个对象中?
我正在这里制作谷歌地图: http://crocdoc.ifas.ufl.edu/projects /chameleonmapdev/
我以这种格式设置了标记的数据:
var nights = ['July1211', 'July1411'];
var waypoint_data = {
July1211: [
//Lat, long, j/a (juvenile/adult)
[25.429363, -80.508326, j],
[25.429332, -80.508216, j]
],
July1411: [
[25.42936, -80.51023, j],
[25.42936, -80.51036, j]
]
};
构建点的函数如下所示:
function buildPoints() {
//var marker_container = new Object;
for ( i = 0; i < nights.length ; i++ ) {
//Loop for each data point that night
for ( h = 0; h < waypoint_data[nights[i]].length; h++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(waypoint_data[nights[i]][h][0], waypoint_data[nights[i]][h][1]),
icon: waypoint_data[nights[i]][h][2],
shape: pointshape,
map: map
});
//Push waypoints into date-specific object key
//marker_container[nights[i]].push(marker);
}
}
}
我想将每个日期 (night[i]) 的标记推送到不同的位置对象键这样我就可以隐藏/显示特定夜晚的标记。我尝试使用注释掉的两行marker_container 行执行此操作,但它们只是破坏了我的循环,并且在Firebug 中,我收到有关marker_container[nights[i]] 未定义的错误。 nights[i] 是一个字符串,所以我认为这种语法可行。非常感谢任何提示,并且我非常乐意接受其他更好的编码方法的建议。
I am working on a Google Map here: http://crocdoc.ifas.ufl.edu/projects/chameleonmapdev/
I have the data for the markers set up in this format:
var nights = ['July1211', 'July1411'];
var waypoint_data = {
July1211: [
//Lat, long, j/a (juvenile/adult)
[25.429363, -80.508326, j],
[25.429332, -80.508216, j]
],
July1411: [
[25.42936, -80.51023, j],
[25.42936, -80.51036, j]
]
};
And the function that builds the points looks like this:
function buildPoints() {
//var marker_container = new Object;
for ( i = 0; i < nights.length ; i++ ) {
//Loop for each data point that night
for ( h = 0; h < waypoint_data[nights[i]].length; h++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(waypoint_data[nights[i]][h][0], waypoint_data[nights[i]][h][1]),
icon: waypoint_data[nights[i]][h][2],
shape: pointshape,
map: map
});
//Push waypoints into date-specific object key
//marker_container[nights[i]].push(marker);
}
}
}
I would like to push the markers for each date (night[i]) into a different object key so that I can hide/show a specific night's markers. I've tried doing this with the two marker_container lines I've commented out, but they just break my loop, and in Firebug, I get an error about marker_container[nights[i]] being undefined. nights[i] is a string, so I thought this syntax would work. Any hints are greatly appreciated, and I'm very open to suggestions for other, better ways to code this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在对象上使用
push
,只能在数组上使用。我认为这就是你想要做的:简单的概念证明 jsfiddle
You can't use
push
on an Object, only on array. I think this is what you want to do:simple proof of concept jsfiddle
如果您知道将有多少个标记(假设有 10 个),请设置标记容器,使对象的每个元素本身成为长度为 2 的对象:
创建标记后,将它们添加到marker_container 中
:想要显示和隐藏标记,请执行以下操作:
If you know how many markers you'll have (let's say 10), set-up the marker container, making each element of the object an object itself, of length 2:
After creating your markers, add them to the marker_container:
When you want to display and hide a marker, do this: