如何将JSON对象数组组合在一起

时间:2018-11-17 15:43:46

标签: jquery json

我有一个JSON文件,在“ delivery_options”下有一个对象数组。

https://api.myjson.com/bins/m0a3m

当前,我正在使用.each提取数据

                $.each(value.delivery_options, function(key, val) {
                        $('#del-options').append('<li del-id='+ key +'  class="list-group-item del-options link-class"><div> ' + val.name + ' </div><div> ' + val.description + ' </div><div class=""> ' + val.price + ' </div></li>');
                })

我的问题是,它将所有交付选项打印在一个列表<ul> </ul>下,而不是将它们分成多个列表<ul> </ul> <ul> </ul> <ul> </ul>

下面是我正在使用的完整jQuery。

    $.ajaxSetup({
        cache: false
    });
    $('#search').blur(function() {
        $('#result').html('');
        $('#lat-long').html('');
        $('#opening-times').html('');
        $('#del-options').html('');
        var searchField = $('#search').val();
        var expression = new RegExp(searchField, "i");
        $.getJSON('https://api.myjson.com/bins/m0a3m', function(data) {
            //console.log('json')
            $.each(data, function(key, value) {

                if (value.address.name.search(expression) != -1 || value.address.line1.search(expression) != -1 || value.address.town.search(expression) != -1 || value.address.county.search(expression) != -1 || value.address.postcode.search(expression) != -1) {
                //console.log(value)
                    //COURIER ADDRESS DETAILS
                    $('#result').append('<li data-contentid='+ key +' class="list-group-item courier"><div class="c-name font-weight-bold"> ' + value.name + ' </div><div class="address"> ' + value.address.name + ',' + value.address.line1 + ',' + value.address.town + ',' + value.address.county + ',' + value.address.postcode + '</div></li>');
                    //LAT AND LONG

                    var mapProp= {
                        center:new google.maps.LatLng(value.location.latitude,value.location.longitude),
                        zoom:5,
                        };
                    var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);



                    //OPENING TIMES
                    $('#opening-times').append('<li id=open-times-'+ key +'  class="list-group-item op-times"><div class="c-name font-weight-bold"> Opening Times </div><div class="">Mon ' + value.opening_times.Mon + ' </div><div class="">Tues ' + value.opening_times.Tues + ' </div><div class="">Wed ' + value.opening_times.Wed + ' </div><div class="">Thurs ' + value.opening_times.Thurs + ' </div><div class="">Fri ' + value.opening_times.Fri + ' </div><div class="">Sat ' + value.opening_times.Sat + ' </div><div class="">Sun ' + value.opening_times.Sun + ' </div></li>');
                    // ARRAY DELIVERY OPTIONS
                    $.each(value.delivery_options, function(key, val) {
                            $('#del-options').append('<li del-id='+ key +'  class="list-group-item del-options link-class"><div> ' + val.name + ' </div><div> ' + val.description + ' </div><div class=""> ' + val.price + ' </div></li>');
                    })

                    //COURIER ADDRESS FOR OTHER CONTAINER
                    $('#address-container').append('<li id=cour-add-'+ key +'  class="list-group-item alt-address link-class"><div class="c-name font-weight-bold"> ' + value.name + ' </div><div class="d-inline-block w-100"> ' + value.address.name + ' </div><div class="d-inline-block w-100"> ' + value.address.line1 + ' </div><div class="d-inline-block w-100"> ' + value.address.town + '</div><div class="d-inline-block w-100"> ' + value.address.county + ' </div><div class="d-inline-block w-100"> ' + value.address.postcode + '</div></li>');
                }
            });
        });
    }); 

这是帮助显示问题的屏幕截图。如您所见,0-5应该在一个列表中,然后另一个0-5在另一个列表中。

https://snag.gy/g2Dtqd.jpg

任何帮助或提示都会很棒。

预先感谢

1 个答案:

答案 0 :(得分:1)

如果将标记更改为

<div id="del-lists-wrapper"></div>

然后将js更改为

var ul = $('<ul></ul>');
$.each(value.delivery_options, function(key, val) {
    ul.append('<li del-id=' + key + '  class="list-group-item del-options link-class"><div> ' + val.name + ' </div><div> ' + val.description + ' </div><div class=""> ' + val.price + ' </div></li>');
});
ul.appendTo('#del-lists-wrapper');

因此您的整个上面的代码将是

$.ajaxSetup({
    cache: false
});
$('#search').blur(function() {
    $('#result').html('');
    $('#lat-long').html('');
    $('#opening-times').html('');
    $('#del-options').html('');
    var searchField = $('#search').val();
    var expression = new RegExp(searchField, "i");
    $.getJSON('https://api.myjson.com/bins/m0a3m', function(data) {
        //console.log('json')
        $.each(data, function(key, value) {

            if (value.address.name.search(expression) != -1 || value.address.line1.search(expression) != -1 || value.address.town.search(expression) != -1 || value.address.county.search(expression) != -1 || value.address.postcode.search(expression) != -1) {
            //console.log(value)
                //COURIER ADDRESS DETAILS
                $('#result').append('<li data-contentid='+ key +' class="list-group-item courier"><div class="c-name font-weight-bold"> ' + value.name + ' </div><div class="address"> ' + value.address.name + ',' + value.address.line1 + ',' + value.address.town + ',' + value.address.county + ',' + value.address.postcode + '</div></li>');
                //LAT AND LONG

                var mapProp= {
                    center:new google.maps.LatLng(value.location.latitude,value.location.longitude),
                    zoom:5,
                    };
                var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);



                //OPENING TIMES
                $('#opening-times').append('<li id=open-times-'+ key +'  class="list-group-item op-times"><div class="c-name font-weight-bold"> Opening Times </div><div class="">Mon ' + value.opening_times.Mon + ' </div><div class="">Tues ' + value.opening_times.Tues + ' </div><div class="">Wed ' + value.opening_times.Wed + ' </div><div class="">Thurs ' + value.opening_times.Thurs + ' </div><div class="">Fri ' + value.opening_times.Fri + ' </div><div class="">Sat ' + value.opening_times.Sat + ' </div><div class="">Sun ' + value.opening_times.Sun + ' </div></li>');
                // ARRAY DELIVERY OPTIONS
                var ul = $('<ul></ul>');
                $.each(value.delivery_options, function(key, val) {
                    ul.append('<li del-id=' + key + '  class="list-group-item del-options link-class"><div> ' + val.name + ' </div><div> ' + val.description + ' </div><div class=""> ' + val.price + ' </div></li>');
                });
                ul.appendTo('#del-lists-wrapper');

                //COURIER ADDRESS FOR OTHER CONTAINER
                $('#address-container').append('<li id=cour-add-'+ key +'  class="list-group-item alt-address link-class"><div class="c-name font-weight-bold"> ' + value.name + ' </div><div class="d-inline-block w-100"> ' + value.address.name + ' </div><div class="d-inline-block w-100"> ' + value.address.line1 + ' </div><div class="d-inline-block w-100"> ' + value.address.town + '</div><div class="d-inline-block w-100"> ' + value.address.county + ' </div><div class="d-inline-block w-100"> ' + value.address.postcode + '</div></li>');
            }
        });
    });
});