开发者

Javascript: Function with part of an array name as a parameter?

开发者 https://www.devze.com 2023-03-23 16:45 出处:网络
I have a Google Map involving several different arrays of data, and I\'m having trouble coding sections of it efficiently. For example, I have this chunk of code, and would like to turn it into a func

I have a Google Map involving several different arrays of data, and I'm having trouble coding sections of it efficiently. For example, I have this chunk of code, and would like to turn it into a function that runs the same thing for various different names instead of just Wilma. I can easily make an array holding the names, and pass that as a parameter, but then how would I build array names like "data_NAME[k][3]"? I imagine it must be simple to concatenate a string with part of an array name, but I'm stuck on syntax. Any help is greatly appreciated.

if (currentcroc == 'Wilma') {

    for (k = 0; k < data_wilma.length; k++) {

        var pointmarker = new google.maps.Marker({
            position: latlng_wilma[k],
            icon: markerType['point'+data_wilma[k][3]],
            shadow: markerType['pointshad开发者_JS百科ow'],
            shape: pointshape,
            map:map,
            zIndex: 4                           
        });

        pointarray.push(pointmarker);


        (function (k, pointmarker) {

            google.maps.event.addListener(pointmarker, 'mouseover', function() {        
                tooltip.show('Wilma: '+data_wilma[k][2]);
            });

            google.maps.event.addListener(pointmarker, 'mouseout', function() {
                tooltip.hide();
            });

        })(k, pointmarker); 

    }   

}


Use an associative array (map) where the key is the name (wilma, for example) and the value is the 2d-array:

var nameToDataMap = {
  wilma: [ [0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]
         ],
  fred: [ [0, 1, 2],
          [3, 4, 5],
          [6, 7, 8]
        ]
};

var nameToLatLongMap = {
   wilma: [ ... ],
   fred: [ ... ]
};

Then your code becomes:

var data = nameToDataMap[currentcroc];
var latLong = nameToLatLongMap[currentcroc];

for (k = 0; k < data.length; k++) {

    var pointmarker = new google.maps.Marker({
        position: latLong[k],
        icon: markerType['point' + data[k][3]],
        shadow: markerType['pointshadow'],
        shape: pointshape,
        map:map,
        zIndex: 4                           
    });

    pointarray.push(pointmarker);

    (function (k, pointmarker) {

        google.maps.event.addListener(pointmarker, 'mouseover', function() {        
            tooltip.show(currentcroc + ': ' + data[k][2]);
        });

        google.maps.event.addListener(pointmarker, 'mouseout', function() {
            tooltip.hide();
        });

    })(k, pointmarker); 
}   


As long as you're accessing the attribute from somewhere (eg. if it is local) you can access attributes as key fields.

Eg.

tooltip.show('Wilma: ' + this['data_' + 'wilma'][k][2]);

Apply variable names and abstractions as necessary :)

0

精彩评论

暂无评论...
验证码 换一张
取 消