简体   繁体   中英

How to filter XML data with the array values in query

My question is how to filter the set of xml data with the an array of values. Here the below jquery code and xml data. kind help to solve the issue. I had used the filter function though its not working

JS - Code

    var data = response.results[1][0].Variant;
    var arrdata = data.split(';');
    arrdata.shift(); // *result : ["1", "2", "3"]*
    function loadfail(){
        alert("Error: Failed to Load Menu Data : XML");
        }
        $.ajax({
        url: '/Prueba/FCD/menudata/menu.xml',  
        dataType: 'xml',
        success: menu,
        error: loadfail
        });
function menu(document){
                $(document).find("variant")
                .filter(function () {
                    return $(this).find('value').text()==arrdata
                })
                .each(function () {
                    var variant_Label = $(this).find('text').text();
                    var variant_Value = $(this).find('value').text();
                    $('#variant_model').append(
                   '<option value="'+ variant_Value + '">' + variant_Label + '</option>'
                    );
                })
            }

XML - Data

<menu>
<variant>
    <value>1</value>
    <text>12 pa</text>
</variant>
<variant>
    <value>2</value>
    <text>30 pa</text>
</variant>
<variant>
    <value>3</value>
    <text>40 pa</text>
</variant>
<variant>
    <value>4</value>
    <text>50 pa</text>
</variant>
</menu>

The XML data is loaded and filtering the dynamic value and attributing it.

$("#variant_model").empty()

                    var data = response1.results[1][0].Variant;
                    var arrdata = data.split(';');
                    arrdata.shift();
                    function loadfail() {
                        alert("Error: Failed to Load Menu Data : XML");
                    }
                    $.ajax({
                        url: '/Prueba/FCD/menudata/menu.xml',
                        success: menu,
                        error: loadfail
                    });
                    console.log(arrdata);

                    function menu(document) {
                        var array2 = arrdata;
                        var array1 = []
                        $(document).find("variant").each(function () {
                            var text = $(this).find('text').text();
                            var value = $(this).find('value').text();
                            var combo = { text, value }
                            array1.push(combo)
                        })
                        array1 = array1.filter(function (item) {
                            return array2.includes(item.value);
                        })

                        array1.forEach(function (item) {
                            $("#variant_model").append($("<option></option>").val(item.value).html(item.text));
                        })
                        $('#variant_model option[value="' + response.item[0][0].Variant + '"]').attr('selected', true);

                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM