js 数组显示问题

JavaScript011

js 数组显示问题,第1张

1、问题不在于你数组的赋值方法。var x =[] ,这样的数值初始化方法是OK。

2、问题在于你的变量名,name 这个词在 javascript 中有特殊的含义。不要用name 作为数组名称,使用其他变量名称即可。

3、具体代码如下所示。

<div id=test></div>

<script>

arr = [[

   {'n':'A','q':'111111'},

   {'n':'B','q':'222222'},

], [

   {'n':'A','q':'333333'},

   {'n':'B','q':'444444'},

]]

document.getElementById("test").innerHTML=arr.map(function(o,i){

   return '数组'+(i+1)+'<ul class="box1">'+o.map(function(o){

      return '<li data-q="'+o.n+'">'+o.q+'</li>'

   }).join('')+'</ul>'

}).join('')

</script>

在js中,可以使用for循环,通过writey方法将数组元素逐个输出在表格中。

新建一个html文件,命名为test.html,用于讲解如何将js数组在table表格中显示出来。在js标签中,创建一个数组,用于测试。在js标签中,使用write方法输出表格标签和行标签,创建一行表格。在js标签中,在表格标签内,使用for循环遍历数组元素。在for循环内,再通过write方法将单元格标签和数组元素值一起输出。在浏览器打开test.html文件,查看结果。