Javascript数组帮助(多维?)

时间:2012-05-12 19:03:37

标签: javascript xml google-maps-api-3

我有一个从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>

1 个答案:

答案 0 :(得分: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