2010-06-10 41 views
2

我有數據的一個表:檢查和在陣列中的jquery替換值

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

我有檢索從上述表中的值到像這樣(0.00,19)

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

我要檢查,如果有一個0.00的值(或者它可能只是0),並更改該值NA ... 所以我的結果數組然後(NA,19) 真的不知道如何去做這件事,無論是在最初的比賽還是作爲一個單獨的行動...

回答

1

對我來說很有意義做map函數中:

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

感謝這個偉大的工作。我喜歡它保持它的功能,我明白了:-) – liz 2010-06-10 19:52:08

0
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

0

做它作爲一個單獨的行動,你可以通過你的陣列循環,檢查是否值是0

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