2016-04-05 10 views
0

テーブルの列でクラスごとに最高の値を取得するにはどうすればよいですか?私は、次のことを試してみました:​​からテーブルで最高の値を得る

HTML

<table> 
    <tr><td class="speed">1.1</td></tr> 
    <tr><td class="speed">3.1</td></tr> 
    <tr><td class="speed">5.5</td></tr> 
    <tr><td class="speed">2.0</td></tr> 
</table> 

のjQuery/Javascriptを

function gethighestspeeds(){ 

    var speeds = $(".speed").map(function() { 
     return parseFloat(this.text, 10); 
    }).get(); 

    var highestspeed = Math.max.apply(Math, speeds); 
    alert(highestspeed) 
} 

をまた、>場合どのように私は一定数以上のすべての値を得ることができますか? $(this).text()代わりのthis.textをごmap機能に:

+1

使用 '.filter()の値< or >一定の値を取得するとき、' – Rayon

+0

は私が ')(' .filterを使用しますか? –

+0

'return parseFloat($(this).text()、10);' –

答えて

0

this.texttd要素のために定義されていません、あなたは解析する必要がありますparseFloat($(this).text(), 10);

function gethighestspeeds() { 
 

 
    var speeds = $(".speed").map(function() { 
 
    return parseFloat($(this).text(), 10); 
 
    }).get(); 
 

 
    var highestspeed = Math.max.apply(Math, speeds); 
 
    snippet.log('high: ' + highestspeed); 
 

 
    var num = 2.3; 
 
    var array = $(".speed").map(function() { 
 
    var flt = parseFloat($(this).text(), 10); 
 
    return flt > num ? flt : undefined; 
 
    }).get(); 
 
    snippet.log('array: ' + array) 
 

 
    //if you already have the speeds array 
 
    var array2 = speeds.filter(function(val) { 
 
    return num < val; 
 
    }); 
 
    snippet.log('array2: ' + array) 
 
} 
 
gethighestspeeds();
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> 
 
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <td class="speed">1.1</td> 
 
    </tr> 
 
    <tr> 
 
    <td class="speed">3.1</td> 
 
    </tr> 
 
    <tr> 
 
    <td class="speed">5.5</td> 
 
    </tr> 
 
    <tr> 
 
    <td class="speed">2.0</td> 
 
    </tr> 
 
</table>

+0

これは素晴らしい動作です!エレガントなソリューション。おかげでArun –

0

は、あなたが使用する必要が

var certainNumber=2.2; //Whatever you want to set 

function gethighestspeeds(){ 

    var speeds = $(".speed").map(function() { 
     return parseFloat(this.text, 10) > parseFloat(certainNumber); 
     }).get(); 


} 
+0

これは 'this.textContent'である必要があります – Rayon

0

........これを試してみてください

return parseFloat($(this).text(), 10); 
+0

私は今間違いを見ます。本当にありがとう! –

関連する問題