2012-05-12 64 views
0

我有一個函數可以將數據從XML文件中提取出來。我試圖將數據存儲在一個數組中,這樣我就可以遍歷數組並對其進行過濾。因此,當數組中的「Time_of_Day」(從下拉菜單)=「Time_of_Day」值時,我的數組將進行過濾。Javascript Array Help(mulit-dimenional?)

現在我只需要幫助建立陣列。然後,我將嘗試瞭解如何循環查找「Time_of_Day」的值。

這裏是我從XML文件中提取數據的函數。現在我的數組只存儲名稱值(主要是因爲我不知道我在做什麼)。

var markerfilter = new Array(); 
MYMAP.placeMarkers = function(filename) { 
$.get(filename, function(xml){ 
    $(xml).find("marker").each(function(){ 
     var name = $(this).find('name').text(); 
     var address = $(this).find('address').text(); 
     var address2 = $(this).find('address2').text(); 
     var Meeting_Type = $(this).find('Meeting_Type').text(); 
     var Time_of_Meeting = $(this).find('Time_of_Meeting').text(); 
     var Day_of_Meeting = $(this).find('Day_of_Meeting').text(); 
     var Open_Meeting = $(this).find('Open_Meeting').text(); 
     var Wheelchair = $(this).find('Wheelchair').text(); 
     var ASL = $(this).find('ASL').text(); 
     var Comments = $(this).find('Comments').text(); 
     markerfilter.push(name); 

     var MeetingType = document.getElementById("Meeting_Type"); 
     var type = MeetingType.options[MeetingType.selectedIndex].text; 
     var DayofMeeting = document.getElementById("Day_of_Meeting"); 
     var day = DayofMeeting.options[DayofMeeting.selectedIndex].text; 
     var TimeofMeeting = document.getElementById("Time_of_Meeting"); 
     var time = TimeofMeeting.options[TimeofMeeting.selectedIndex].text; 

     // create a new LatLng point for the marker 
     var lat = $(this).find('lat').text(); 
     var lng = $(this).find('lng').text(); 
     var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng)); 

     // extend the bounds to include the new point 
     MYMAP.bounds.extend(point); 

     var marker = new google.maps.Marker({ 
      position: point, 
      map: MYMAP.map 
     }); 

     var infoWindow = new google.maps.InfoWindow(); 
     var html='<b><u>'+name+'</b></u><br />'+address2+'<br />'+address+'<br />'+Meeting_Type+',&nbsp'+Time_of_Meeting+',&nbsp'+Day_of_Meeting+'<br />Open Meeting:&nbsp'+Open_Meeting+'<br />Wheelchair Accessible:&nbsp'+Wheelchair+'<br />ASL:&nbsp'+ASL+'<br />Comments:&nbsp'+Comments; 
     google.maps.event.addListener(marker, 'click', function() { 
      infoWindow.setContent(html); 
      infoWindow.open(MYMAP.map, marker); 
     }); 
     MYMAP.map.fitBounds(MYMAP.bounds); 
     }); 
    }); 
} 

所創建看起來像這樣的XML文件:

<markers> 
    <marker> 
     <name>AA at Greek Food</name> 
     <address>292 Danforth Avenue Toronto, ON</address> 
     <address2>Asteria</address2> 
     <Meeting_Type>AA</Meeting_Type> 
     <Time_of_Meeting>Late</Time_of_Meeting> 
     <Day_of_Meeting>Wednesday</Day_of_Meeting> 
     <Open_Meeting>Yes</Open_Meeting> 
     <Wheelchair>Yes</Wheelchair> 
     <ASL>Yes</ASL> 
     <Comments>Greek food is good.</Comments> 
     <lat>43.677322</lat> 
     <lng>-79.353729</lng> 
    </marker> 
    <marker> 
     <name>CA over Sushi</name> 
     <address>18 Jane Street, Toronto, ON</address> 
     <address2>ASA Sushi</address2> 
     <Meeting_Type>CA</Meeting_Type> 
     <Time_of_Meeting>Early</Time_of_Meeting> 
     <Day_of_Meeting>Monday</Day_of_Meeting> 
     <Open_Meeting>No</Open_Meeting><Wheelchair>Yes</Wheelchair> 
     <ASL>No</ASL> 
     <Comments>CA eating Sushi.</Comments> 
     <lat>43.649773</lat> 
     <lng>-79.484772</lng> 
    </marker> 
<markers> 
+0

什麼是您的XML是什麼樣子? –

回答

1

你可能想要做這樣的事情:

$(xml).find("marker").each(function() { 
    markerfilter.push({ 
     name: $(this).find("name").text(), 
     address: $(this).find("address").text() 
     // and so on 
    }); 
}); 
console.log(markerfilter);​ 

// the markerfilter array will look like: 
// 
// [ 
// {name: "name1", address: "address1", /*...*/}, 
// {name: "name2", address: "address2", /*...*/}, 
// . 
// . 
// . 
// ] 

編輯

看你的XML,我認爲創建這樣的關聯數組要容易得多:

$(xml).find("marker").each(function() { 
    var markerdata = {}; 
    $(this).children().each(function() { 
     markerdata[this.tagName] = $(this).text(); 
    }); 
    markerfilter.push(markerdata); 
}); 
console.log(markerfilter); 

demo here

+0

非常感謝! –