如何为foreach jQuery中的每个数组添加新列

如何为foreach jQuery中的每个数组添加新列,jquery,json,Jquery,Json,我有三个数组作为json返回,我想在一个新列中显示每个数组。我怎样才能做到这一点 这是$.post函数 $.post("/booking/times", { id: $("#user_id").val(), selectedDay: formattedDate }, function(data) { console.log(data); $('#dayTimes').empty(); $(".loading-icon

我有三个数组作为json返回,我想在一个新列中显示每个数组。我怎样才能做到这一点

这是
$.post
函数

  $.post("/booking/times", {
      id: $("#user_id").val(),
      selectedDay: formattedDate
    },
    function(data) {
      console.log(data);
      $('#dayTimes').empty();
      $(".loading-icon").hide();


      if (data.times.length == 0) {
           console.log("no available times");
      } else {
        $.each(data.times, function(index, value) {
          console.log(value);
        });
      }
    });
下面是我从
console.log中获得的json输出,它位于
每个
中:

{  
    "times":[  
        [  
            "10:30 - 11:00",
            "11:00 - 11:30",
            "11:30 - 12:00",
            "12:00 - 12:30",
            "12:30 - 13:00",
            "13:00 - 13:30",
            "13:30 - 14:00"
        ],
        [  
            "14:00 - 14:30",
            "14:30 - 15:00",
            "15:00 - 15:30",
            "15:30 - 16:00",
            "16:00 - 16:30",
            "16:30 - 17:00"
        ],
        [  
            "17:00 - 17:30",
            "17:30 - 18:00",
            "18:00 - 18:30",
            "18:30 - 19:00",
            "19:00 - 19:30",
            "19:30 - 20:00"
        ]
    ]
}

@拉库尔卡。在新列中显示每个数组是什么意思

如果要在html表中将数组中的值显示为字符串。然后试试下面的内容

$.post("/booking/times", {
      id: $("#user_id").val(),
      selectedDay: formattedDate
    },
    function(data) {

      var body = document.getElementsByTagName('body')[0];
      console.log(data);
      $('#dayTimes').empty();
      $(".loading-icon").hide();


      if (data.times.length == 0) {
           console.log("no available times");
      } else {

        var tbl = document.createElement('table');
        tbl.style.width = '100%';
        tbl.setAttribute('border', '1');
        var tbdy = document.createElement('tbody');
        var tr = document.createElement('tr');
        $.each(data.times, function(index, value) {
            var td = document.createElement('td');
            td.appendChild(document.createTextNode(value.toString()))
            tr.appendChild(td)
        });
        tbdy.appendChild(tr);
        tbl.appendChild(tbdy);
        body.appendChild(tbl)
      }
    });

@拉库尔卡。在新列中显示每个数组是什么意思

如果要在html表中将数组中的值显示为字符串。然后试试下面的内容

$.post("/booking/times", {
      id: $("#user_id").val(),
      selectedDay: formattedDate
    },
    function(data) {

      var body = document.getElementsByTagName('body')[0];
      console.log(data);
      $('#dayTimes').empty();
      $(".loading-icon").hide();


      if (data.times.length == 0) {
           console.log("no available times");
      } else {

        var tbl = document.createElement('table');
        tbl.style.width = '100%';
        tbl.setAttribute('border', '1');
        var tbdy = document.createElement('tbody');
        var tr = document.createElement('tr');
        $.each(data.times, function(index, value) {
            var td = document.createElement('td');
            td.appendChild(document.createTextNode(value.toString()))
            tr.appendChild(td)
        });
        tbdy.appendChild(tr);
        tbl.appendChild(tbdy);
        body.appendChild(tbl)
      }
    });

试着把它放在你的$中。每一个,我都对它进行采样,然后输出到一个表中

output = '<table border="1">';
$.each(value, function(index, row) {
    output += '<tr>';
    $.each(row, function(index, cell) {
        output += '<td>' + cell + '</td>';
    });
    output += '</tr>';
});
output += '</table>';
$('#div_element').html(output);
output='';
$.each(值、函数(索引、行){
输出+='';
$.each(行、函数(索引、单元格){
输出+=''+单元+'';
});
输出+='';
});
输出+='';
$('#div_元素').html(输出);

试着把它放在你的$中。每一次,我都对它进行采样,并将其输出到一个表中

output = '<table border="1">';
$.each(value, function(index, row) {
    output += '<tr>';
    $.each(row, function(index, cell) {
        output += '<td>' + cell + '</td>';
    });
    output += '</tr>';
});
output += '</table>';
$('#div_element').html(output);
output='';
$.each(值、函数(索引、行){
输出+='';
$.each(行、函数(索引、单元格){
输出+=''+单元+'';
});
输出+='';
});
输出+='';
$('#div_元素').html(输出);
试试这个 时间是你的数组

var表=$(“”);
变量thead=$(“”);
var tbody=$(“”);
var trhead=$(“”);
对于(var i=0;i试试这个
时间是你的数组

var表=$(“”);
变量thead=$(“”);
var tbody=$(“”);
var trhead=$(“”);

对于(var i=0;i我最终使用的是这个

    $.each(data.times, function(index, value) {
      // console.log(value);
      var timesColumns = '<div class="col-md-4">';
      $.each(value, function(key, value) {
        // console.log(value);
        timesColumns += '<div class="pretty superhoidjad-booking "><input name="available_times[]" class="available_times" type="checkbox" value="' + value + '"/><label><i class="mdi mdi-check"></i>' + value + '</label></div>';
        // console.log(times);
      });
      timesColumns += "</div>";
      console.log(timesColumns);
      $("#dayTimes").append(timesColumns);

    });
$.each(数据、时间、函数(索引、值){
//console.log(值);
var timesColumns='';
$.each(值、函数(键、值){
//console.log(值);
timesColumns+=''+值+'';
//console.log(次);
});
timesColumns+=“”;
console.log(times列);
$(“#dayTimes”).append(times列);
});

我最后用的是这个

    $.each(data.times, function(index, value) {
      // console.log(value);
      var timesColumns = '<div class="col-md-4">';
      $.each(value, function(key, value) {
        // console.log(value);
        timesColumns += '<div class="pretty superhoidjad-booking "><input name="available_times[]" class="available_times" type="checkbox" value="' + value + '"/><label><i class="mdi mdi-check"></i>' + value + '</label></div>';
        // console.log(times);
      });
      timesColumns += "</div>";
      console.log(timesColumns);
      $("#dayTimes").append(timesColumns);

    });
$.each(数据、时间、函数(索引、值){
//console.log(值);
var timesColumns='';
$.each(值、函数(键、值){
//console.log(值);
timesColumns+=''+值+'';
//console.log(次);
});
timesColumns+=“”;
console.log(times列);
$(“#dayTimes”).append(times列);
});

根据您的示例json数据,您想在3列中显示吗?@juntapao是的,先生。根据您的示例json数据,您想在3列中显示吗?@juntapao是的,先生。