Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.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数组映射到html表_Javascript_Html - Fatal编程技术网

将javascript数组映射到html表

将javascript数组映射到html表,javascript,html,Javascript,Html,我想创建一个函数,将网格(表示为嵌套数组)映射到html表 例如,以列表为例: [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['f', 'g', 'h'] ] 并更改html表格以显示: a b c d e f f g h 假设表大小预先确定,列表与维度匹配 因此,完成以下工作: table border="1"> <tr> <td>row 1, cell 1</td> <td>row

我想创建一个函数,将网格(表示为嵌套数组)映射到html表

例如,以列表为例:

[
    ['a', 'b', 'c'],
    ['d', 'e', 'f'],
    ['f', 'g', 'h']
]
并更改html表格以显示:

a b c 
d e f
f g h
假设表大小预先确定,列表与维度匹配

因此,完成以下工作:

table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>


<script>
update = [['a','b'],['c','d']]

function updateTableHTML(myArray) {
  // maps the list onto the existing table
}

updateTableHTML(update)
</script>
table border=“1”>
第1行,第1单元
第1行第2单元
第2行第1单元
第2行,第2单元
更新=['a','b',['c','d']]
函数updateTableHTML(myArray){
//将列表映射到现有表
}
updateTableHTML(更新)
执行此操作

var a = [
   ['a', 'b', 'c'],
   ['d', 'e', 'f'],
   ['f', 'g', 'h']
];
var html = "<table>";
for(var i in a){
    html += "<tr>";
      for(var j in a[i])
        html += "<td>"+a[i][j]+"</td>";
    html += "</tr>";
}

html += "</table>";
document.write(html);
var a=[
[a',b',c'],
[d'、[e'、[f'],
['f','g','h']
];
var html=“”;
对于(a中的var i){
html+=“”;
对于(a[i]中的var j)
html++=“a[i][j]+”;
html+=“”;
}
html+=“”;
document.write(html);
执行此操作

var a = [
   ['a', 'b', 'c'],
   ['d', 'e', 'f'],
   ['f', 'g', 'h']
];
var html = "<table>";
for(var i in a){
    html += "<tr>";
      for(var j in a[i])
        html += "<td>"+a[i][j]+"</td>";
    html += "</tr>";
}

html += "</table>";
document.write(html);
var a=[
[a',b',c'],
[d'、[e'、[f'],
['f','g','h']
];
var html=“”;
对于(a中的var i){
html+=“”;
对于(a[i]中的var j)
html++=“a[i][j]+”;
html+=“”;
}
html+=“”;
document.write(html);
执行此操作

var a = [
   ['a', 'b', 'c'],
   ['d', 'e', 'f'],
   ['f', 'g', 'h']
];
var html = "<table>";
for(var i in a){
    html += "<tr>";
      for(var j in a[i])
        html += "<td>"+a[i][j]+"</td>";
    html += "</tr>";
}

html += "</table>";
document.write(html);
var a=[
[a',b',c'],
[d'、[e'、[f'],
['f','g','h']
];
var html=“”;
对于(a中的var i){
html+=“”;
对于(a[i]中的var j)
html++=“a[i][j]+”;
html+=“”;
}
html+=“”;
document.write(html);
执行此操作

var a = [
   ['a', 'b', 'c'],
   ['d', 'e', 'f'],
   ['f', 'g', 'h']
];
var html = "<table>";
for(var i in a){
    html += "<tr>";
      for(var j in a[i])
        html += "<td>"+a[i][j]+"</td>";
    html += "</tr>";
}

html += "</table>";
document.write(html);
var a=[
[a',b',c'],
[d'、[e'、[f'],
['f','g','h']
];
var html=“”;
对于(a中的var i){
html+=“”;
对于(a[i]中的var j)
html++=“a[i][j]+”;
html+=“”;
}
html+=“”;
document.write(html);
您可以使用,也可以使用简单的for循环。您只需逐步遍历二维数组的每个组件,并在表中创建一个
元素

顺便说一句,如果表已经有了正确数量的行和列(即
update
数组符合表的维度),我会觉得不舒服:最好从头开始重新构建
,以避免任何错误并创建更灵活的函数。让我们假设这样一个初始情况:

<table>
    <tbody id="your-table-body-id">
        <!-- whatever... -->
    </tbody>
</table>
a
b
c
奇特的
数组
方法 下面是一个使用
Array.prototype.forEach
方法的示例:

function updateTableHTML(myArray) {
    var tableBody = document.getElementById("your-table-body-id");

    // Reset the table
    tableBody.innerHTML = "";

    // Build the new table
    myArray.forEach(function(row) {
        var newRow = document.createElement("tr");
        tableBody.appendChild(newRow);

        if (row instanceof Array) {
            row.forEach(function(cell) {
                var newCell = document.createElement("td");
                newCell.textContent = cell;
                newRow.appendChild(newCell);
            });
        } else {
            newCell = document.createElement("td");
            newCell.textContent = row;
            newRow.appendChild(newCell);
        }
    });
}
笔记 请注意,
Array.prototype.forEach
方法可能不在每个浏览器中都受支持(即Internet Explorer<9)。
for
方法看起来更容易理解(虽然我不确定哪一种更快),但这是您的选择

另外,如果您想知道:我正在检查if(row instanceof Array),因为在如下情况下:

update = ["a", "b", "c"];
我假设你想要这样的结果:

<table>
    <tbody id="your-table-body-id">
        <!-- whatever... -->
    </tbody>
</table>
a
b
c
因此,在再次循环并构建每行的单元格之前,必须检查该值是否为数组。

您可以使用,也可以使用简单的for循环。您只需逐步遍历二维数组的每个组件,并在表中创建一个
元素

顺便说一句,如果表已经有了正确数量的行和列(即
update
数组符合表的维度),我会觉得不舒服:最好从头开始重新构建
,以避免任何错误并创建更灵活的函数。让我们假设这样一个初始情况:

<table>
    <tbody id="your-table-body-id">
        <!-- whatever... -->
    </tbody>
</table>
a
b
c
奇特的
数组
方法 下面是一个使用
Array.prototype.forEach
方法的示例:

function updateTableHTML(myArray) {
    var tableBody = document.getElementById("your-table-body-id");

    // Reset the table
    tableBody.innerHTML = "";

    // Build the new table
    myArray.forEach(function(row) {
        var newRow = document.createElement("tr");
        tableBody.appendChild(newRow);

        if (row instanceof Array) {
            row.forEach(function(cell) {
                var newCell = document.createElement("td");
                newCell.textContent = cell;
                newRow.appendChild(newCell);
            });
        } else {
            newCell = document.createElement("td");
            newCell.textContent = row;
            newRow.appendChild(newCell);
        }
    });
}
笔记 请注意,
Array.prototype.forEach
方法可能不在每个浏览器中都受支持(即Internet Explorer<9)。
for
方法看起来更容易理解(虽然我不确定哪一种更快),但这是您的选择

另外,如果您想知道:我正在检查if(row instanceof Array),因为在如下情况下:

update = ["a", "b", "c"];
我假设你想要这样的结果:

<table>
    <tbody id="your-table-body-id">
        <!-- whatever... -->
    </tbody>
</table>
a
b
c
因此,在再次循环并构建每行的单元格之前,必须检查该值是否为数组。

您可以使用,也可以使用简单的for循环。您只需逐步遍历二维数组的每个组件,并在表中创建一个
元素

顺便说一句,如果表已经有了正确数量的行和列(即
update
数组符合表的维度),我会觉得不舒服:最好从头开始重新构建
,以避免任何错误并创建更灵活的函数。让我们假设这样一个初始情况:

<table>
    <tbody id="your-table-body-id">
        <!-- whatever... -->
    </tbody>
</table>
a
b
c
奇特的
数组
方法 下面是一个使用
Array.prototype.forEach
方法的示例:

function updateTableHTML(myArray) {
    var tableBody = document.getElementById("your-table-body-id");

    // Reset the table
    tableBody.innerHTML = "";

    // Build the new table
    myArray.forEach(function(row) {
        var newRow = document.createElement("tr");
        tableBody.appendChild(newRow);

        if (row instanceof Array) {
            row.forEach(function(cell) {
                var newCell = document.createElement("td");
                newCell.textContent = cell;
                newRow.appendChild(newCell);
            });
        } else {
            newCell = document.createElement("td");
            newCell.textContent = row;
            newRow.appendChild(newCell);
        }
    });
}
笔记 请注意,
Array.prototype.forEach
方法可能不在每个浏览器中都受支持(即Internet Explorer<9)。
for
方法看起来更容易理解(虽然我不确定哪一种更快),但这是您的选择

另外,如果您想知道:我正在检查if(row instanceof Array),因为在如下情况下:

update = ["a", "b", "c"];
我假设你想要这样的结果:

<table>
    <tbody id="your-table-body-id">
        <!-- whatever... -->
    </tbody>
</table>
a
b
c
因此,在再次循环并构建每行的单元格之前,必须检查该值是否为数组。

您可以使用,也可以使用简单的for循环。您只需逐步遍历二维数组的每个组件,并在表中创建一个
元素

顺便说一句,如果表已经有了正确数量的行和列(即
update
数组符合表的维度),我会觉得不舒服:最好从头开始重新构建
,以避免任何错误并创建更灵活的函数。让我们假设这样一个初始情况:

<table>
    <tbody id="your-table-body-id">
        <!-- whatever... -->
    </tbody>
</table>
a
b
c
奇特的
数组
方法 这里有一个关于t的例子