将长javascript结合成一个

时间:2012-03-13 08:35:03

标签: javascript for-loop

我正在尝试将一个非常长的javascript代码转换为一个单独的“for循环:从A点到B点,我做错了什么?这是一个调用谷歌地图标记,每个标记使用不同的内容和位置,但他们共享相同的图标,阴影......下面的点A:

var mappro1 = new google.maps.Marker({
                    position: map1,
                    map: map,
                    icon: companyImage,
                    shadow: companyShadow,
                    title:"c",
                    zIndex: 4
            });
            var mappro2 = new google.maps.Marker({
                    position: map2,
                    map: map,
                    icon: companyImage,
                    shadow: companyShadow,
                    title:"c",
                    zIndex: 4
            });
            var mappro3 = new google.maps.Marker({
                    position: map3,
                    map: map,
                    icon: companyImage,
                    shadow: companyShadow,
                    title:"c",
                    zIndex: 4
            });
            var mappro4 = new google.maps.Marker({
                    position: map4,
                    map: map,
                    icon: companyImage,
                    shadow: companyShadow,
                    title:"c",
                    zIndex: 4
            });
            var mappro5 = new google.maps.Marker({
                    position: map5,
                    map: map,
                    icon: companyImage,
                    shadow: companyShadow,
                    title:"c",
                    zIndex: 4
            });
            var mappro6 = new google.maps.Marker({
                    position: map6,
                    map: map,
                    icon: companyImage,
                    shadow: companyShadow,
                    title:"c",
                    zIndex: 4
            });
            var mappro7 = new google.maps.Marker({
                    position: map7,
                    map: map,
                    icon: companyImage,
                    shadow: companyShadow,
                    title:"c",
                    zIndex: 4
            });
            var mappro8 = new google.maps.Marker({
                    position: map8,
                    map: map,
                    icon: companyImage,
                    shadow: companyShadow,
                    title:"c",
                    zIndex: 4
            });
            var mappro9 = new google.maps.Marker({
                    position: map9,
                    map: map,
                    icon: companyImage,
                    shadow: companyShadow,
                    title:"c",
                    zIndex: 4
            });

以下B点:

            for ($i=1; $i <10; $i++){

                var $mappros = "mappro" + $i;
                var $mappos = "map" + $i;

                var mappros = new google.maps.Marker({
                    position: $mappos,
                    map: map,
                    icon: companyImage,
                    shadow: companyShadow,
                    title:"$i",
                    zIndex: $i
                });
            };

1 个答案:

答案 0 :(得分:1)

根据A:

中的代码,这是我的循环变体
var mappros = [];
for (var $i = 1; $i < 10; $i++){
    var mappro = new google.maps.Marker({
        position: "map" + $i,
        map: map,
        icon: companyImage,
        shadow: companyShadow,
        title: "c",
        zIndex: 4
    });
    mappros.push(mappro);
};

因此,您将获得一个mappro数组,以通过索引访问任何一个。如果你需要为每个mappro单独命名,你应该用这个替换标题行:

title: $i,