views:

111

answers:

2

I have a table:

<table class="datatable" id="hosprates">
                <caption> hospitalization rates test</caption> <thead>
                <tr>
                    <th scope="col">Funding Source</th> <th scope="col">Alameda County</th> <th scope="col">California</th>
                </tr>
                </thead>
                <tbody>
                    <tr>
                        <th scope="row">Medi-Cal</th>
                        <td>34.3</td>
                        <td>32.3</td>
                    </tr>
                    <tr>
                        <th scope="row">Private</th>
                        <td>32.2</td>
                        <td>34.2</td>
                    </tr>
                    <tr>
                        <th scope="row">Other</th>
                        <td>22.7</td>
                        <td>21.7</td>
                    </tr>
                </tbody>
            </table>

i want to retrieve column 1 and column 2 values per row as pairs that end up looking like this [funding,number],[funding,number]

i did this so far, but when i alert it, it only shows [object, object]...

  var myfunding =  $('#hosprates tbody tr').each(function(){
  var funding = new Object();

  funding.name = $('#hosprates tbody tr td:nth-child(1)').map(function() {
              return $(this).text().match(/\S+/)[0];
             }).get();
  funding.value= $('#hosprates tbody tr td:nth-child(2)').map(function() {
              return $(this).text().match(/\S+/)[0];
             }).get();

});
alert (myfunding);
+2  A: 
var result = $('#hosprates tbody').children().map(function () {
    var children = $(this).children();

    return {
       name: children.eq(0).text(),
       value: children.eq(1).text()
    };
}).get();

This will build an array in the form:

[
 { name : "...", value: "..." },
 { name : "...", value: "..." },
 { name : "...", value: "..." }
]

etc

To get the name of the first row, use:

alert(result[0].name);

For the value:

alert(result[0].value);

Edit: if you want the result EXACTLY as you specify:

var result = $('#hosprates tbody').children().map(function () {
    var children = $(this).children();

    return "[" + children.eq(0).text() + "," + children.eq(1).text() + "]"
}).get().join(",");
Matt
hi matt, thanks, much simpler.. but i need the results returned formatted the way i described above... would i then do a each function on result.name and result.value to format each pair? or on result?
liz
@liz: If that is the case then you only need to tweek @Matts code
Sean Kinsey
okay but see the curly brackets, those seem to be generated automatically and i need [] strait brackets flanking each pair. or is the return {} part of the content and not bracketing that piece of code?
liz
@liz: See my edit :)
Matt
+1 Nice work :)
fudgey
that's great matt, i tried something like that myself but it kept failing, i didnt think to use the join().
liz
A: 

Try this then (demo):

var funding = $('#hosprates tbody tr').map(function(){
    return [[ $(this).find('th').eq(0).text() , // find first and only <th>
            $(this).find('td').eq(0).text() ]]; // find first <td> in the row
    }).get();

   alert(funding);  // Output: Medi-Cal,32.3,Private,34.2,Other,21.7

The alert display only shows the data inside the array, it is actually formatted like this:

[["Medi-Cal", "32.3"], ["Private", "34.2"], ["Other", "21.7"]]

So you could get the Medi-Cal data like this:

alert(funding[0][0] + ' -> ' + funding[0][1]);  // output: Medi-Cal -> 34.3
fudgey
thanks but, this returns medical,32.3,private,34.2,other,21.7
liz
sorry it wont let me edit that comment which is wrong... i see now you are saying the alert wont show me the data, but the thing is i need to grab the text values and format them exactly as my example without the quotes or extra spaces.
liz
sorry - i am not being clear. i have a lot of these pairs not just 3, it seems like there should be a way to output the data exactly as i want automatically, without manually calling funding[0][0] etc... it doesnt seem like the most efficient way to do it.
liz
I guess I don't understand how you want your output... You want your output to have square brackets and commas, so it's all text? And from your question, I wasn't sure what words you were stripping out (you only want the first word?). If your output is all text, it'll be much more difficult to get the values unless you make both an array and the string of text.
fudgey