开发者

checking and replacing a value in an array jquery

i have a table of data:

 <table id="disparities" class="datatable">
                       <th开发者_如何学Cead>
                        <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...


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


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)


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';
}


var points1 = $('#disparities td:nth-child(2)').map(function() {
  return $(this).text().replace(/(0[\.0]?0?)/, "NA");
}).get();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜