简体   繁体   English

在javascript中将html表转换为数组

[英]Convert html table to array in javascript

How can an HTML table be converted into a JavaScript array?如何将 HTML 表格转换为 JavaScript 数组?

<table id="cartGrid">
  <thead>
       <tr>
          <th>Item Description</th>
          <th>Qty</th>
          <th>Unit Price</th>
          <th>Ext Price</th>
       </tr>
  </thead>
<tbody>
    <tr><td>Old Lamp</td><td>1</td><td>107.00</td><td>107.00</td>
    <tr><td>Blue POst</td><td>2</td><td>7.00</td><td>14.00</td>
</tbody>
</table>

Here's one example of doing what you want.这是一个做你想做的事的例子。

var myTableArray = [];

$("table#cartGrid tr").each(function() {
    var arrayOfThisRow = [];
    var tableData = $(this).find('td');
    if (tableData.length > 0) {
        tableData.each(function() { arrayOfThisRow.push($(this).text()); });
        myTableArray.push(arrayOfThisRow);
    }
});

alert(myTableArray);

You could probably expand on this, say, using the text of the TH to instead create a key-value pair for each TD.您可能会对此进行扩展,例如,使用 TH 的文本来代替为每个 TD 创建一个键值对。

Since this implementation uses a multidimensional array, you can access a row and a td by doing something like this:由于此实现使用多维数组,因此您可以通过执行以下操作来访问行和 td:

myTableArray[1][3] // Fourth td of the second tablerow

Edit: Here's a fiddle for your example: http://jsfiddle.net/PKB9j/1/编辑:这是您的示例的小提琴:http: //jsfiddle.net/PKB9j/1/

This a function coverts an Html Table (just give the id) and it returns an array of the table :这个函数转换了一个 Html 表(只需给出 id)并返回一个表数组:

            function table_to_array(table_id) {
                    myData = document.getElementById(table_id).rows
                    //console.log(myData)
                    my_liste = []
                    for (var i = 0; i < myData.length; i++) {
                            el = myData[i].children
                            my_el = []
                            for (var j = 0; j < el.length; j++) {
                                    my_el.push(el[j].innerText);
                            }
                            my_liste.push(my_el)

                    }
                    return my_liste
            }

I hope it helps you !我希望它对你有帮助!

You can write also with key value pair.您也可以使用键值对编写。 this is so simple.这很简单。

function htmlTableDataToArrayOfObject(){

    var arrayOfThisRow = [];
            $("#cartGrid tbody tr").each(function() {
                
                debugger;
                var description = $(this).find('td:eq(0)').text();
                var qty = $(this).find('td:eq(1)').text();
                var unitPrice= $(this).find('td:eq(2)').text();
    
                arrayOfThisRow.push({
                         desc: description,
                         quantity: qty,
                         price: unitPrice
                    });
            });
}

Here's typescript solution:这是打字稿解决方案:


function getTableContent(table: HTMLTableElement): string[][] {
    return Array.from(table.rows).reduce((acc, row) => {
        const columns = Array.from(row.children)
            .map(column => column.textContent || '')

        return acc.concat(columns);
    }, [] as string[][])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM