简体   繁体   中英

How to get value from other variable

How to get value from other variable in JavaScript?

<script type="text/javascript"> <!--

    var lat0 = 10.212; 
    var lat1 = 11.9090; 
    var lat2 = 12.12;

    var lon0 = 10.212; 
    var lon1 = 11.9090; 
    var lon2 = 12.12;

    var lat = new Array("lat"+i); 
    var lon = new Array("lon"+i);

    for (var i = 0; i < 2; i++) {   
        lat[i] = "lat"+i;   
        lon[i] = "lon"+i;       
        document.write(lat[i]);     
        document.write(" ");    
        document.write(lon[i]);     
        document.write(" "); 
    }
    //--> 
</script>

I want to get lat0, lat1, lat2, lon0, lon1, lon2 values.

Normally I'd suggest that you'd use bracket notation but you should really just use the right data structure from the beginning:

var positions = [
    {lat: 10.212, lon: 10.212},
    {lat: 11.9090, lon: 11.9090},
    {lat: 12.12, lon: 12.12}
];

var pos;

for (var i=0; i<positions.length; i++) {
    pos = positions[i];
    document.write(pos.lat + ' ' + pos.lng + ' ');
}

NB be very judicious with document.write . It's almost always better to use DOM manipulation instead. See Why is document.write considered a "bad practice"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM