Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript在数组中循环10次_Javascript_Arrays - Fatal编程技术网

Javascript在数组中循环10次

Javascript在数组中循环10次,javascript,arrays,Javascript,Arrays,我对javascript不是很在行,我正在努力通过从ajax请求传回的一些数据创建一个循环 我想要的是循环遍历数组十次,并在表中生成一行 然而,它似乎不起作用。下面是完整的代码 $.getJSON('charts_ajax.php',{a : 'terms'},function(data){ if(data){ var tableTop = '<tbody><tr><th width="5%">#</th><th width=

我对javascript不是很在行,我正在努力通过从ajax请求传回的一些数据创建一个循环

我想要的是循环遍历数组十次,并在表中生成一行

然而,它似乎不起作用。下面是完整的代码

$.getJSON('charts_ajax.php',{a : 'terms'},function(data){
   if(data){
     var tableTop = '<tbody><tr><th width="5%">#</th><th width="65%">Search Terms</th><th width="15%">Visits</th></tr>';

     var si = 1;
     $.each(data, function(index, value) {
         var tableInner = '<tr><td>1</td><td>' + data[si][0]  + '</td><td>307</td></tr>';
         si++;
     });

     var tableBottom = '</tbody></table>';

     $('#terms-table').html(tableTop + tableInner + tableBottom);

   }

});
我在这里是个彻头彻尾的傻瓜吗


提前干杯,伙计们:)

您在每次通话中都会重新分配tableInner。你的意思是这样做吗

var tableInner = '';
$.each(data, function(index, value) {
         tableInner += '<tr><td>1</td><td>' + data[si][0]  + '</td><td>307</td></tr>';
         si++;
});
那么这个,

$.each(data, function(index, value) {
  var part1 = value[0];
  var part2 = value[1];
  console.log(part1 + ' ' + part2);
});
产出:

"Terms Visits"
"radio fm 150"
"radio fm grimsby 25"
"radio  10"
"radio fm radio 9"
"radio .co.uk 9"
"grimsby rugby club 8"
"radio radio 7"
"radio radio grimsby 5"
"radio  5"
"radio station 4"

迭代数据的方式存在错误。

因此,对于任何查看此数据的人,我都通过一点帮助解决了问题,感谢@remyabel指出我正在重新迭代TableInternal变量,而不是添加到它

以下是解决方案(带注释的完整代码):

///创建请求
$.getJSON('charts_ajax.php',{a:'terms'},函数(数据){
//检查是否有从请求返回的数据
如果(数据){
//启动表格html
var tableTop=“#搜索项visits”;
var tableInner='';
//为数据中的每个项创建表的内行
$.each(数据、函数(索引、行){
如果(索引!==0){
tableInner+=''+索引+''+行[0]+''+行[1]+'';
}
});
//合上桌子
var tableBottom='';
//按id查找表并插入html。
$(“#术语表”).html(桌面+桌面内部+桌面底部);
}
});

我确实需要这样做是的。当我将其添加到中时,我现在得到:Uncaught TypeError:无法读取未定义的属性“0”,也无法读取任何表displayed@MarkP这取决于数组的设置方式。
si
计数器变量不是不必要的吗@MarkP可以使用
参数或
@remyabel解决了问题,请将我的回答作为答案-也向您表示感谢。:)
$.each(data, function(index, value) {
  var part1 = value[0];
  var part2 = value[1];
  console.log(part1 + ' ' + part2);
});
"Terms Visits"
"radio fm 150"
"radio fm grimsby 25"
"radio  10"
"radio fm radio 9"
"radio .co.uk 9"
"grimsby rugby club 8"
"radio radio 7"
"radio radio grimsby 5"
"radio  5"
"radio station 4"
/// Create the Request
$.getJSON('charts_ajax.php',{a : 'terms'},function(data){
   // Check that there is data coming back from the request
   if(data){
       //Start the table html
       var tableTop = '<tbody><tr><th width="5%">#</th><th width="65%">Search Terms</th><th width="15%">Visits</th></tr>';
       var tableInner = '';

       //For each of the items in data Create an inner row of the table
       $.each(data, function(index, row) {
          if(index !== 0){
          tableInner += '<tr><td>' + index + '</td><td>' + row[0]  + '</td><td>' + row[1] + '</td></tr>';
          }
       });

      //close the table
      var tableBottom = '</tbody></table>';

     // Find the table by id and insert the html. 
     $('#terms-table').html(tableTop + tableInner + tableBottom);

     }

});