2

I have a table and I need to get values for each row (in array), something like: [{1,'test'}, {2,'another test'}, {3, 'nothing'}], but the only I can get is: ["1 test", "2 another test","3 nothing"]. I need to send this values by ajax as json object ... and create another table - thus I need an array of values ...

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()
});

the html is something like this:

<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>

I really need help ...

2
  • 2
    There is no such construct in JavaScript as {1,'test'} - did you mean object {1: 'test'} or array [1, 'test']? Commented Nov 27, 2011 at 9:25
  • sorry .. I know only basics ... I just knew what I'd needed in server side :( jfriend00 already gave me the answer. Thnx anyway ) Commented Nov 27, 2011 at 10:22

1 Answer 1

4

This form of data makes no sense as it's very hard to read with javascript:

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

I'm going to assume that what you want is this:

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

To get that second form, you could use this code:

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

Working example here: http://jsfiddle.net/jfriend00/WjgrC/.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! You were right about the format - is just that I didn't know how to ask :) Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.