0

我有一个从 XML 文件中提取数据的函数。我正在尝试将该数据存储在一个数组中,这样我就可以遍历该数组并对其进行过滤。因此,当数组中的“Time_of_Day”(来自下拉菜单)=“Time_of_Day”值时,我的数组将过滤。

现在我只需要帮助构建阵列。然后我将尝试了解如何遍历它并找到“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>
4

1 回答 1

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);

演示在这里

于 2012-05-12T19:24:30.640 回答