Is there an easy way to create dynamic variables with Javascript?

后端 未结 5 452
你的背包
你的背包 2020-12-01 07:06

I\'ve built a data-driven google map with different icons that get assigned to the map depending on the type of item located. So if I have 5 types of landmark, and each gets

5条回答
  •  -上瘾入骨i
    2020-12-01 07:22

    You'd be better off creating a javascript object which you can use somewhat like an associative array is used in PHP:

    var types = ['hospital','church','library','store'];
    var landmarks= {};
    for (var i in types) {
        landmarks[types[i]]= new google.maps.Icon();
        landmarks[types[i]].image = "icon" + i + ".png";
    } 
    alert(landmarks['hospital'].image);  // displays "icon0.png"
    

提交回复
热议问题