Javascript 如何在html表格列中找到最低值
我用datatables jquery插件动态创建了这个html表 因此,我:Javascript 如何在html表格列中找到最低值,javascript,jquery,datatable,Javascript,Jquery,Datatable,我用datatables jquery插件动态创建了这个html表 因此,我: <tbody> <tr role="row" class="odd"> <td class="sorting_1">2015-09-29 15:33:09</td> <td>1230</td> <td></td> <td></td>
<tbody>
<tr role="row" class="odd">
<td class="sorting_1">2015-09-29 15:33:09</td>
<td>1230</td>
<td></td>
<td></td>
<td>All inclusive</td>
</tr>
<tr role="row" class="even">
<td class="sorting_1">2015-09-29 16:01:03</td>
<td>1309</td>
<td></td>
<td></td>
<td>nma</td>
</tr>
<tr role="row" class="odd">
<td class="sorting_1">2015-09-29 16:01:03</td>
<td>1900</td>
<td></td>
<td></td>
<td>nema</td>
</tr>
<tr role="row" class="even">
<td class="sorting_1">2015-09-29 16:08:17</td>
<td>2000</td>
<td></td>
<td></td>
<td>nema poruka za sada</td>
</tr>
<tr role="row" class="odd">
<td class="sorting_1">2015-09-29 16:32:54</td>
<td>3900</td>
<td></td>
<td></td>
<td>to je to</td>
</tr>
<tr role="row" class="even">
<td class="sorting_1">2015-09-29 16:32:57</td>
<td>1200</td>
<td></td>
<td></td>
<td>+magacin</td>
</tr>
<tr role="row" class="odd">
<td class="sorting_1">2015-09-29 17:43:58</td>
<td>3400</td>
<td>2015-09-29</td>
<td>2015-09-30</td>
<td>+skladiste + niza cena + tacan da tum utovara</td>
</tr>
</tbody>
如何找到data.cena的最低值?首先,我建议更新您的代码片段,使其更具可读性。这可能会鼓励更多的人回答 第二,jQuery方法:可以循环遍历表行,获取每个行中的第二个td,并将值存储在数组中。然后,您可以跟踪数组以获取最低值
var values = [];
var lowestVal = 0;
$('tr').each(function(){
var value = $(this).children('td').eq(1).html();
lowestVal = value;
values.push(value);
});
for(var x = 0; x < values.length; x++){
if(values[x] < lowestVal)lowestVal = values[x];
}
console.log(lowestVal);
var值=[];
var lowestVal=0;
$('tr')。每个(函数(){
var值=$(this.children('td').eq(1.html();
lowestVal=值;
值。推送(值);
});
对于(var x=0;x
编辑
啊,我看你已经更新了你的代码片段。谢谢:)
Edit2:已更新以修复错误。已测试。首先,我建议更新您的代码片段,使其更具可读性。这可能会鼓励更多的人回答 第二,jQuery方法:可以循环遍历表行,获取每个行中的第二个td,并将值存储在数组中。然后,您可以跟踪数组以获取最低值
var values = [];
var lowestVal = 0;
$('tr').each(function(){
var value = $(this).children('td').eq(1).html();
lowestVal = value;
values.push(value);
});
for(var x = 0; x < values.length; x++){
if(values[x] < lowestVal)lowestVal = values[x];
}
console.log(lowestVal);
var值=[];
var lowestVal=0;
$('tr')。每个(函数(){
var值=$(this.children('td').eq(1.html();
lowestVal=值;
值。推送(值);
});
对于(var x=0;x
编辑
啊,我看你已经更新了你的代码片段。谢谢:)
Edit2:已更新以修复错误。已测试。我建议使用
:第n个类型(2)
来选择每个
中的第二个
元素
$('trtd:nth of type(2)')
应该抓住第二个
元素
$('tr td:nth of type(2)')。text()
将返回文本值,因此,1230、1309等。我建议使用:nth of type(2)
来选择每个
中的第二个
元素
$('trtd:nth of type(2)')
应该抓住第二个
元素
$('tr td:nth of type(2)')。text()将返回文本值,因此,1230、1309等将使用和,如下所示
var lowest = $("tr td").map(function() {
//When it's the 2nd td in every row
if ( $(this).index() === 1 ) {
return $(this).text();
}
}).get().sort(function(a, b) {
return a - b;
}).shift(); //get the first item out
var lowest = $.map(json.data, function(node) {
return node.cena;
}).sort(function(a, b) {
return a - b;
}).shift(); //get the first item out
编辑:
对于您发布的JSON,您可以执行与上面相同的操作,只需稍作更改,如下所示
var lowest = $("tr td").map(function() {
//When it's the 2nd td in every row
if ( $(this).index() === 1 ) {
return $(this).text();
}
}).get().sort(function(a, b) {
return a - b;
}).shift(); //get the first item out
var lowest = $.map(json.data, function(node) {
return node.cena;
}).sort(function(a, b) {
return a - b;
}).shift(); //get the first item out
编辑:
还有一个简单的方法:
var lowest = Infinity; //The value Infinity (positive infinity) is greater than any other number;
json.data.forEach(function(node) {
lowest = node.cena < lowest && node.cena || lowest;
});
var最低=无穷大//值无穷大(正无穷大)大于任何其他数字;
forEach(函数(节点){
最低=node.cena<最低和&node.cena | |最低;
});
使用和,如下所示
var lowest = $("tr td").map(function() {
//When it's the 2nd td in every row
if ( $(this).index() === 1 ) {
return $(this).text();
}
}).get().sort(function(a, b) {
return a - b;
}).shift(); //get the first item out
var lowest = $.map(json.data, function(node) {
return node.cena;
}).sort(function(a, b) {
return a - b;
}).shift(); //get the first item out
编辑:
对于您发布的JSON,您可以执行与上面相同的操作,只需稍作更改,如下所示
var lowest = $("tr td").map(function() {
//When it's the 2nd td in every row
if ( $(this).index() === 1 ) {
return $(this).text();
}
}).get().sort(function(a, b) {
return a - b;
}).shift(); //get the first item out
var lowest = $.map(json.data, function(node) {
return node.cena;
}).sort(function(a, b) {
return a - b;
}).shift(); //get the first item out
编辑:
还有一个简单的方法:
var lowest = Infinity; //The value Infinity (positive infinity) is greater than any other number;
json.data.forEach(function(node) {
lowest = node.cena < lowest && node.cena || lowest;
});
var最低=无穷大//值无穷大(正无穷大)大于任何其他数字;
forEach(函数(节点){
最低=node.cena<最低和&node.cena | |最低;
});
您可以使用以下脚本:
var values = [];
$('table tr td:nth-child(2)').each(function () {
var value = parseFloat($(this).text());
values.push(value);
});
console.log(Math.min.apply(Math,values));
您可以使用以下脚本:
var values = [];
$('table tr td:nth-child(2)').each(function () {
var value = parseFloat($(this).text());
values.push(value);
});
console.log(Math.min.apply(Math,values));
首先,让我们将您的tbody
包装在表中
以使事情顺利进行。之后,您可以使用此代码来获得所需的结果
首先定义一个min
函数,您可以在代码中需要integermin
功能的任何其他地方使用该函数。然后我们只需遍历所有第二列条目,并将它们作为整数值存储在数组中
最后,我们在数组中找到最小值,在本例中在控制台中显示它
Array.prototype.min = function () {
return Math.min.apply(Math, this);
};
var colVals = [];
$('tbody tr td:nth-child(2)').each(function () {
colVals.push(parseInt($(this).text()));
});
console.log(colVals.min());
编辑
对于JSON,您可以这样做
Array.prototype.min = function () {
return Math.min.apply(Math, this);
};
var values = [];
for(var i = 0; i < response['data'].length; i++){
values.push(response['data'][i].cena);
}
console.log(values.min());
Array.prototype.min=函数(){
返回Math.min.apply(Math,this);
};
var值=[];
对于(var i=0;i
首先,让我们将您的t正文
包装在表格中
以使事情顺利进行。之后,您可以使用此代码来获得所需的结果
首先定义一个min
函数,您可以在代码中需要integermin
功能的任何其他地方使用该函数。然后我们只需遍历所有第二列条目,并将它们作为整数值存储在数组中
最后,我们在数组中找到最小值,在本例中在控制台中显示它
Array.prototype.min = function () {
return Math.min.apply(Math, this);
};
var colVals = [];
$('tbody tr td:nth-child(2)').each(function () {
colVals.push(parseInt($(this).text()));
});
console.log(colVals.min());
编辑
对于JSON,您可以这样做
Array.prototype.min = function () {
return Math.min.apply(Math, this);
};
var values = [];
for(var i = 0; i < response['data'].length; i++){
values.push(response['data'][i].cena);
}
console.log(values.min());
Array.prototype.min=函数(){
返回Math.min.apply(Math,this);
};
var值=[];
对于(var i=0;i
要回答您的第一个问题,“如何从表列中获取最低值?”我将提供以下函数作为一种方法,尽管我已对其进行了扩展,以识别并返回具有最小值、最大值和最接近平均值的
元素:
2015-09-29 15:33:09
1230
一价全包
2015-09-29 16:01:03
1309
nma
2015-09-29 16:01:03
1900
内马
2015-09-29 16:08:17
2000
内玛·波鲁卡·扎萨达
2015-09-29 16:32:54
3900
对
2015-09-29 16:32:57
1200
+马加辛
2015-09-29 17:43:58
3400
2015-09-29
2015-09-30
+斯卡拉迪斯特+尼扎塞纳+塔康塔姆乌托瓦拉
要回答您的第一个问题,“如何从表列中获取最低值?”我将提供以下函数作为一种方法,尽管我已经