簡體   English   中英

為 javascript 動態創建數組

[英]Create array dynamically for javascript

我正在嘗試在谷歌 map 上顯示標記。如果我使用 static 數組作為標記的緯度和經度,它工作正常。

但是當我嘗試使用 php 數組進行動態設置時,它就不起作用了。

這是代碼。

 $(document).ready(function(){
   var var_locations = <?=json_encode($locations)?>;

/****** Array which need to make Dynamic *********/
var locations=[
        ['Location <br>Name', 13.0104292, 77.64844689999995, 1],
        ['Location <br>Name', 28.6699553, 77.10752720000005, 1],
    ];
    function initialize() {
        console.log(locations);
        var mapOptions = {
            center: new google.maps.LatLng(20.5937, 78.9629),
            zoom: 3,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
        };
        var map = new  google.maps.Map(document.getElementById("map"),mapOptions);

        var  i;
        

        for (i = 0; i < locations.length; i++) {
            
          var infoWindow = new google.maps.InfoWindow({
                content: locations[i][0],
            });
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                map: map,
                title:locations[i][0]
            });  
            google.maps.event.addListener(marker, 'click', (function(mm, tt) {
        return function() {
            infoWindow.setContent(tt);
            infoWindow.open(map, mm);
        }
    })(marker, locations[i][0]));

        }
        

    }
google.maps.event.addDomListener(window, 'load', initialize);    

});

我希望上面代碼中的以下數組是動態的

var locations=[
    ['Location <br>Name', 13.0104292, 77.64844689999995, 1],
    ['Location <br>Name', 28.6699553, 77.10752720000005, 1],
];

為此,我使用以下代碼使其動態化

var var_locations = <?=json_encode($locations)?>;
var locations=[
       var_locations.forEach(function(item) {
            [item.clinicName, item.latitude, item.longitude],
        });
    ];

但它不起作用。 為了測試循環是否工作,我嘗試在循環中打印控制台值( item.latitude ),並且正在打印值,意味着循環工作正常。

但是,如果我在 js 數組中使用循環,那么它就不起作用了。 也沒有給出我可以識別問題的錯誤。

如何使該數組動態化?

更新

[
{
    "clinicName": "Dummy clinic",
    "doctorName": "Any name",
    "phoneNumber": 234242342,
    "address": "test address, city",
    "latitude": "13.0104292",
    "email": "user@gmail.com",
    "longitude": "77.64844689999995"
},
{
    "clinicName": "Dummy clinic",
    "doctorName": "Any name",
    "phoneNumber": 234242342,
    "address": "test address, city",
    "latitude": "13.0104292",
    "email": "user@gmail.com",
    "longitude": "77.64844689999995"
},
]

這是 json 數組,我從 api 獲取。我將該數組轉換為 php 數組,以便在我的頁面上使用。

forEach 不返回任何內容。 此外,您在客戶端中沒有任何 JSON。 已經是一個object

你需要一個 map - 除非你簡單地創建數組,因為它應該看起來像在服務器上

在這里我還將緯度/經度轉換為浮點數

 const locations = [ { "clinicName": "Dummy clinic", "doctorName": "Any name", "phoneNumber": 234242342, "address": "test address, city", "latitude": "13.0104292", "email": "user@gmail.com", "longitude": "77.64844689999995" }, { "clinicName": "Dummy clinic", "doctorName": "Any name", "phoneNumber": 234242342, "address": "test address, city", "latitude": "13.0104292", "email": "user@gmail.com", "longitude": "77.64844689999995" }, ].map(item => ([item.clinicName, +item.latitude, +item.longitude])) console.log(locations)

假設 JSON 是一個對象數組。


const json = [{
  clinicName:"Location 1",
  latitude:13.0104292,
  longitude: 77.64844689999995
},{
  clinicName:"Location 2",
  latitude:13.0104292,
  longitude: 77.64844689999995
}];

const locations = json.map(location => ([location.clinicName, location.latitude,location.longitude ]));
console.log(locations);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM