Convert 2D array to HTML table using Javascript
P粉009828788
P粉009828788 2023-08-13 17:05:44
0
1
530
<p>I have a 2D array in my Javascript. The array is assigned to the variable 'arr' and looks like this: </p> <pre class="brush:php;toolbar:false;">[ [ "0", { cat_id: "1", cat_name: "Tiger", }, ], [ "1", { cat_id: "2", cat_name: "Lion", }, ], [ "2", { cat_id: "3", cat_name: "Cheetah", }, ], [ "3", { cat_id: "5", cat_name: "Leopard", }, ], ]</pre> <p>I want to give users a neat HTML table on the screen, with keys as headers and values ​​as rows, as follows: </p> <table class="s-table"> <thead> <tr> <th>cat_id</th> <th>cat_name</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Tiger</td> </tr> <tr> <td>2</td> <td>Lion</td> </tr> <tr> <td>3</td> <td>Cheetah</td> </tr> <tr> <td>4</td> <td>Leopard</td> </tr> </tbody> </table> <p>I've looked at a lot of articles on StackOverflow and around the web, but I'm not a Javascript expert and I honestly can't get anything to work.非常感谢任何帮助!</p> <p>我的HTML中包含了空表格代码:</p> <p><br /></p> <pre class="snippet-code-js lang-js prettyprint-override"><code>const arr = [["0", { cat_id: "1", cat_name: "Tiger", },], ["1", { cat_id: "2", cat_name: "Lion", },], ["2", { cat_id: "3", cat_name: "Cheetah", },], ["3", { cat_id: "5", cat_name: "Leopard", },],] var tableBody = document.getElementById("wrap"); tableBody.innerHTML = ""; arr.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); } });</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code><table id="wrap"></table></code></pre> <p><br /></p> <p>因为arr是一个二维数组,所以我的代码目前只会产生以下表格:</p> <pre class="brush:php;toolbar:false;">0 [object Object] 1 [object Object] 2 [object Object] 3 [object Object]</pre> <p>我明白<em>为什么</em>会发生这种情况,但我的JS水平不足以实现我想要的结果。</p>
P粉009828788
P粉009828788

reply all(1)
P粉226413256

Here is an example that iterates over the Object.entries of each nested data object, first building the header row from the keys and then the data rows from the values.

const arr = [
  ["0", { cat_id: "1", cat_name: "Tiger" }],
  ["1", { cat_id: "2", cat_name: "Lion" }],
  ["2", { cat_id: "3", cat_name: "Cheetah" }],
  ["3", { cat_id: "5", cat_name: "Leopard" }],
];

var tableBody = document.getElementById("wrap");

tableBody.innerHTML = "";

const data = arr.map(a =>
  Object.entries(a[1]).sort(([a], [b]) => a.localeCompare(b))
);

const headerRow = document.createElement("tr");
tableBody.appendChild(headerRow);
for (const [key] of data[0]) {
  const newCell = document.createElement("td");
  newCell.textContent = key;
  headerRow.appendChild(newCell);
}

for (const datum of data) {
  const newRow = document.createElement("tr");
  tableBody.appendChild(newRow);
  for (const [, value] of datum) {
    const newCell = document.createElement("td");
    newCell.textContent = value;
    newRow.appendChild(newCell);
  }
}
<table id="wrap"></table>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!