views:

45

answers:

4

i have a table of data:

 <table id="disparities" class="datatable">
                       <thead>
                        <tr>
                            <th scope="col">Events</th> <th scope="col">White</th> <th scope="col">Black</th> <th scope="col">Hispanic</th><th scope="col">Asian/Pacific Islands</th>
                        </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <th scope="row">Hospitalizations</th>
                                <td>0.00</td>
                                <td>20</td>
                                <td>10</td>
                                <td>5</td>
                            </tr>
                            <tr>
                                <th scope="row">ED Visits</th>
                                <td>19</td>
                                <td>90</td>
                                <td>40</td>
                                <td>18</td>
                            </tr>
                        </tbody>
                    </table>

i have a function that retrieves the values from the above table into an array like so (0.00,19)

 var points1 =   $('#disparities td:nth-child(2)').map(function() {
        return $(this).text().match(/\S+/)[0];
       }).get();

i want to check if there is a 0.00 value (or it could be just 0) and change that value to NA... so my resulting array is then (NA,19) not really sure how to go about this, whether in the initial match or as a separate action...

A: 
var points1 = $('#disparities td:nth-child(2)').map(function() {
    var t = $(this).text().match(/\S+/)[0];
    if (parseFloat(t) === .0) return 'NA';
    else return t;
}).get();

(jsFiddle)

Tgr
A: 

Doing it as a separate action, you can just loop through your array and check if a value is 0.

for (var i = 0; i < points1.length; i++)
{
    if (points1[i] == 0)
        points[i] = 'NA';
}
rosscj2533
+1  A: 

Makes sense to me to do it within the map function:

 var points1 =   $('#disparities td:nth-child(2)').map(function() {
        var point = $(this).text().match(/\S+/)[0];
         return (point == 0)?'NA':point;
}).get();
Ryley
thanks this worked great. i liked that it kept it within the function and i understood it :-)
liz
A: 
var points1 = $('#disparities td:nth-child(2)').map(function() {
  return $(this).text().replace(/(0[\.0]?0?)/, "NA");
}).get();
safetycopy