将表转换为数组

时间:2011-11-22 17:49:06

标签: javascript jquery arrays html-table

我已经看过很多关于如何将数组转换成表格的帖子,但其他方式的数据并不多。我想找一张这样的桌子:


<table id="dataTable">
    <tr>
        <th>Functional Category</th>
        <th>Brand Name</th>
        <th>When Obtained</th>
        <th>How Obtained</th>
        <th>How Often Worn</th>
        <th>Where Made</th>
        <th>Has a Graphic</th>
    </tr>
    <tr>
        <td>T-Shirt</td>
        <td>threadless</td>
        <td>Last 3 Months</td>
        <td>Purchased</td>
        <td>Monthly</td>
        <td>India</td>
        <td>Yes</td>
    </tr>
    <tr>
        <td>T-Shirt</td>
        <td>RVCA</td>
        <td>2 Years Ago</td>
        <td>Purchased</td>
        <td>Bi-Monthly</td>
        <td>Mexico</td>
        <td>Yes</td>
    </tr>
</table>

进入这样的数组:


var tableData = [
    {
        category: "T-shirt",
        brandName: "threadless",
        whenObtained: "Last 3 Months",
        howObtained: "Purchased",
        howOftenWorn: "Monthly",
        whereMade: "India",
        hasGraphic: "Yes"
    },
    {
        category: "T-shirt",
        brandName: "RVCA",
        whenObtained: "2 Years Ago",
        howObtained: "Purchased",
        howOftenWorn: "Bi-Monthly",
        whereMade: "Mexico",
        hasGraphic: "Yes"
    }
]

我希望使用jQuery,并想知道最好的方法是什么。

5 个答案:

答案 0 :(得分:12)

以下应该这样做!

var array = [];
var headers = [];
$('#dataTable th').each(function(index, item) {
    headers[index] = $(item).html();
});
$('#dataTable tr').has('td').each(function() {
    var arrayItem = {};
    $('td', $(this)).each(function(index, item) {
        arrayItem[headers[index]] = $(item).html();
    });
    array.push(arrayItem);
});

See here for jsFiddle

答案 1 :(得分:5)

var table = document.getElementById( "dataTable" );
var tableArr = [];
for ( var i = 1; i < table.rows.length; i++ ) {
    tableArr.push({
        category: table.rows[i].cells[0].innerHTML,
        brandName: table.rows[i].cells[1].innerHTML,
        whenObtained: table.rows[i].cells[2].innerHTML,
        howObtained: table.rows[i].cells[3].innerHTML,
        howOftenWorn: table.rows[i].cells[4].innerHTML,
        whereMade: table.rows[i].cells[5].innerHTML,
        hasGraphic: table.rows[i].cells[6].innerHTML
    });
}

答案 2 :(得分:1)

有关示例,请参阅here。我已经在下面列出了相关代码:

$(function() {
    var $rows= $("#tableName tbody tr");      
    var data = [];

    $rows.each(function(row, v) {
        $(this).find("td").each(function(cell, v) {
            if (typeof data[cell] === 'undefined') {
                data[cell] = [];
            }
            data[cell][row] = $(this).text();
        });
    });

    console.log(data);
});

答案 3 :(得分:1)

所以我做的是选择所有tr元素并迭代它们。接下来我检查以确保我没有查看th元素。然后我使用cols数组来填充对象。这可能更简单(2d数组),但我使用cols,因为这是你的输出。

var i, items, item, dataItem, data = [];
var cols = [ "category", "brandName", "whenObtained", "howObtained", "howOftenWorn",
 "whereMade", "hasGraphic" ];

$("#dataTable tr").each(function() {
    items = $(this).children('td');

    if(items.length === 0) { // return if this tr only contains th
        return;
    }

    dataItem = {};
    for(i = 0; i < cols.length; i+=1) {
        item = items.eq(i);
        if(item) {
            dataItem[cols[i]] = item.html();
        }
    }
    data.push(dataItem);
});

答案 4 :(得分:0)

有一个很好的工具:$(table).map,但值得一提的是,无论内部嵌套了多少个.map(),它总是返回扁平数组(您需要两个:一个用于行,一个用于单元格)。 / p>

因此,我们将jQuery.map用于表,将tr's和object.map()用于单元格,使其具有第二个嵌套级别返回一个数组

function t2a (tabble){
return $.map($(tabble),function(el,i) {
    return $.map($(el).find('tr'),function(el,i) {
        return [
            $(el).find('td').map(function() {
                return $(this).text();
            }).get()
        ];
    });
});
}