There is a table, cells are not always filled in it, that is, there are empty ones. How to find the smallest number in each of the lines?
2 answers
It's simple: we go around all the lines in a cycle, inside which we go around all the cells and compare them to .text()
. At the end of the traversal of the next line, we have the smallest of values.
The code might look like this:
live example on jsfiddle: https://jsfiddle.net/ipshenicyn/51u348nc/
$('table tr').each(function(){ var $tr = $(this); var smallest = null; $tr.find('td').each(function(){ var number = parseInt($(this).text(), 10); if(number && (smallest === null || smallest > number)) smallest = number; }); if(null !== smallest){ $tr.find('.result').text(smallest); } });
For the sake of example, I write the smallest value in the specially allocated for it last cell of every row (bold).
If you interpret your question in more detail - it will be possible to give an answer that is appropriate for your situation (if this option does not suit you).
- Thanks, it was already done right. - YarNik
- I did not spend anything, I upgraded it for myself, I looked at another answer that was higher (now it’s lower, tried to figure it out), thank you and again I looked at another answer (it finds in the whole table and it doesn’t work), so you have a plus and closed the question. - YarNik
- Sorry for the sharpness. Misunderstood you. I decided that you mean "no longer needed, I did everything." - Ivan Pshenitsyn
|
var min = Number.MAX_SAFE_INTEGER; $('td').each(function(){ if ($.isNumeric($(this).html())) if ($(this).html() < min) min = parseInt($(this).html()); }); alert(min);
td { border:1px solid #ccc; padding: 10px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <table class="jsTable"> <tbody> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> 5 </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> 2 </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> 1 </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> 4 </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> 6 </td> <td> </td> </tr> </tbody> </table>
|