Javascript:在循环内生成对象键并将对象数组推送到每个对象中?

发布于 2024-12-05 02:30:35 字数 1488 浏览 0 评论 0原文

我正在这里制作谷歌地图: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆橘 2024-12-12 02:30:35

您不能在对象上使用 push,只能在数组上使用。我认为这就是你想要做的:

function buildPoints() {
    var marker_container = new Object();

        for ( i = 0; i < nights.length ; i++ ) {
            marker_container[nights[i]] = new Array();

            //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);

            }

        }

    }   

简单的概念证明 jsfiddle

You can't use push on an Object, only on array. I think this is what you want to do:

function buildPoints() {
    var marker_container = new Object();

        for ( i = 0; i < nights.length ; i++ ) {
            marker_container[nights[i]] = new Array();

            //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);

            }

        }

    }   

simple proof of concept jsfiddle

冰雪之触 2024-12-12 02:30:35

如果您知道将有多少个标记(假设有 10 个),请设置标记容器,使对象的每个元素本身成为长度为 2 的对象:

    marker_container= new Object(10);
       for (i=0; i<marker_container.length; i++){
          marker_container[i]=new Object(2);
       }

创建标记后,将它们添加到marker_container 中

  marker_container[i][0] = nights[i];
  marker_container[i][1] = marker;

:想要显示和隐藏标记,请执行以下操作:

  //display
  marker_container['nightX'].setMap(map);

  //hide
  marker_container['nightX'].setMap(null);

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:

    marker_container= new Object(10);
       for (i=0; i<marker_container.length; i++){
          marker_container[i]=new Object(2);
       }

After creating your markers, add them to the marker_container:

  marker_container[i][0] = nights[i];
  marker_container[i][1] = marker;

When you want to display and hide a marker, do this:

  //display
  marker_container['nightX'].setMap(map);

  //hide
  marker_container['nightX'].setMap(null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文