javascript - 如何分割jquery数组

标签 javascript jquery arrays html-table

我有一个表,我需要获取每一行的值(在数组中),例如: [{1,'test'}, {2,'另一个测试'}, {3, 'nothing'}],但我唯一能得到的是: [“1 个测试”,“2 个另一个测试”,“3 没有”]。我需要通过 ajax 将这些值作为 json 对象发送...并创建另一个表 - 因此我需要一个值数组...

var table = $("table");
var rowsAll = table.find("tbody tr");
rowsAll.each(function(i,l) {
    var rows = [];
    $(this).each(function(t,l){
    rows[rows.length] = cells[t]= $(this).text();
    return rows; //returns rows, but I need cells of each row
    }).serialize();//or get()
});

html 是这样的:

<table id="deal">
<thead>
    <tr>
        <th> num </th>
        <th> string </th>
    </tr>
</thead>
<tbody>
    <tr> <td> 1 </td> <td> test </td> </tr>
    <tr> <td> 2 </td> <td> another test </td> </tr>
    <tr> <td> 3 </td> <td> nothing </td> </tr>
</tbody>
</table>

我真的需要帮助......

最佳答案

这种形式的数据没有任何意义,因为用 javascript 很难读取:

[{1,'test'}, {2,'another test'}, {3, 'nothing'}]

我假设你想要的是这样的:

{"1": "test", "2": "another test", "3": "nothing"}

要获得第二种形式,您可以使用以下代码:

var results = {};
$("#deal tbody tr").each(function() {
    var cols = $(this).find("td");
    results[$.trim(cols.eq(0).text())] = $.trim(cols.eq(1).text());
});

此处的工作示例:http://jsfiddle.net/jfriend00/WjgrC/ .

关于javascript - 如何分割jquery数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8284516/

相关文章:

javascript - Vue js 1.0在另一个方法中调用一个方法会抛出 'not a function'

javascript - 如何在 Phaser JS 中实现冲刺能力?

javascript - 将模态框定位到当前窗口 - jQuery

javascript - 如何根据表单值更改div id名称?

javascript - 如何在 JavaScript 中创建高性能、不可变的数组?

c# - 字符串到字节数组(到字符串到 XML)并再次返回

javascript - 将 d3.nest rollup 应用于嵌套饼图(以改变饼图的大小)

javascript - BigVideo.js Uncaught ReferenceError : _V_ is not defined

javascript - 在 MVC 中使用 jquery 更改 foreach 循环中的 CSS 类

c - 为什么动态二维字符数组中的值被覆盖?