Jquery 如何将对象的JSON数组组合在一起
我有一个JSON文件,它在“delivery_options”下有一个对象数组 目前,我正在使用.each提取此数据Jquery 如何将对象的JSON数组组合在一起,jquery,json,Jquery,Json,我有一个JSON文件,它在“delivery_options”下有一个对象数组 目前,我正在使用.each提取此数据 $.each(value.delivery_options, function(key, val) { $('#del-options').append('<li del-id='+ key +' class="list-group-item del-options link-class"&
$.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>');
})
$。每个(value.delivery_选项、功能(键、值){
$(“#del options”).append(“”+val.name+“”+val.description+“”+val.price+” ”);
})
我的问题是,它将所有交付选项打印在一个列表下,而不是将它们分成多个列表
下面是我使用的完整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>');
}
});
});
});
$.ajaxSetup({
缓存:false
});
$('#search').blur(函数(){
$('#result').html('');
$('#lat long').html('');
$(“#开放时间”).html(“”);
$('#del options').html('');
var searchField=$('#search').val();
var表达式=新的RegExp(searchField,“i”);
$.getJSON('https://api.myjson.com/bins/m0a3m,函数(数据){
//log('json')
$。每个(数据、函数(键、值){
if(value.address.name.search(expression)!=-1 | value.address.line1.search(expression)!=-1 | value.address.town.search(expression)!=-1 | value.address.country.search(expression)!=-1 | value.address.postcode.search(expression)!=-1){
//console.log(值)
//快递地址详情
$(“#result”).append(““+value.name+”+value.address.name+”、“+value.address.line1+”、“+value.address.town+”、“+value.address.country+”、“+value.address.postcode+” );
//纵横
var mapProp={
中心:新的google.maps.LatLng(值.位置.纬度,值.位置.经度),
缩放:5,
};
var map=new google.maps.map(document.getElementById(“googleMap”),mapProp);
//开放时间
$(“#开放时间”).append(“开放时间周一+value.Opening_Times.Mon+“周二”+value.Opening_Times.Tues+“周三”+value.Opening_Times.Wed+“周四”+value.Opening_Times.Thurs+“周五”+value.Opening_Times.Fri+“周六”+value.Opening_Times.Sat+“周日”+value.Opening_Times.Sun+“ ”;
//阵列交付选项
$.each(value.delivery_选项、函数(键、值){
$(“#del options”).append(“”+val.name+“”+val.description+“”+val.price+” ”);
})
//其他集装箱的快递地址
$(“#地址容器”).append(“”+value.name+“”+value.address.name+“”+value.address.line1+“”+value.address.town+“”+value.address.country+“”+value.address.postcode+” ”);
}
});
});
});
下面是一个屏幕截图,有助于显示问题。正如您所看到的,0-5应该在一个列表中,然后另一个0-5在另一个列表中
任何帮助或提示都会很好
如果将标记更改为,请提前感谢
<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');
var ul=$('
');
$.each(value.delivery_选项、函数(键、值){
ul.append(“”+val.name+“”+val.description+“”+val.price+” ”);
});
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>');
}
});
});
});
$.ajaxSetup({
缓存:false
});
$('#search').blur(函数(){
$('#result').html('');
$('#lat long').html('');
$(“#开放时间”).html(“”);
$('#del options').html('');
var searchField=$('#search').val();
var表达式=新的RegExp(searchField,“i”);
$.getJSON('https://api.myjson.com/bins/m0a3m,函数(数据){
//log('json')
$。每个(数据、函数(键、值){
if(value.address.name.search(expression)!=-1 | value.address.line1.search(expression)!=-1 | value.address.town.search(expression)!=-1 | value.address.country.search(expression)!=-1 | value.address.postcode.search(expression)!=-1){
//console.log(值)
//快递地址详情
$(“#result”).append(““+value.name+”+value.address.name+”、“+value.address.line1+”、“+value.address.town+”、“+value.address.country+”、“+value.address.postcode+” );
//纵横
var mapProp={
中心:新的google.maps.LatLng(值.位置.纬度,值.位置.经度),
缩放:5,
};
var map=new google.maps.map(document.getElementById(“googleMap”),mapProp);
//开放时间
$(“#开放时间”).append(“开放时间周一+value.Opening_Times.Mon+“周二”+value.Opening_Times.Tues+“周三”+value.Opening_Times.Wed+“周四”+value.Opening_Times.Thurs+“周五”+value.Opening_Times.Fri+“周六”+value.Opening_Times.Sat+“周日”+value.Opening_Times.Sun+“ ”;
//阵列交付选项
var ul=$(“
”);
$.each(value.delivery_选项、函数(键、值){
ul.append(“”+val.nam