Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript查找最低值_Javascript_Json_Multidimensional Array - Fatal编程技术网

使用javascript查找最低值

使用javascript查找最低值,javascript,json,multidimensional-array,Javascript,Json,Multidimensional Array,这里是输入 [{"Organisation unit":"Barisal Division","BCG Coverage (EPI) Jan to Jun 2014":130.3,"BCG Coverage (EPI) Jul to Dec 2014":112.7}, {"Organisation unit":"Chittagong Division","BCG Coverage (EPI) Jan to Jun 2014":118.4,"BCG Coverage (EPI) Jul to De

这里是输入

[{"Organisation unit":"Barisal Division","BCG Coverage (EPI) Jan to Jun 2014":130.3,"BCG Coverage (EPI) Jul to Dec 2014":112.7},
{"Organisation unit":"Chittagong Division","BCG Coverage (EPI) Jan to Jun 2014":118.4,"BCG Coverage (EPI) Jul to Dec 2014":122.4},
{"Organisation unit":"Dhaka Division","BCG Coverage (EPI) Jan to Jun 2014":112.9,"BCG Coverage (EPI) Jul to Dec 2014":123.3},
{"Organisation unit":"Khulna Division","BCG Coverage (EPI) Jan to Jun 2014":126.9,"BCG Coverage (EPI) Jul to Dec 2014":113.2},
{"Organisation unit":"Rajshahi Division","BCG Coverage (EPI) Jan to Jun 2014":168.5,"BCG Coverage (EPI) Jul to Dec 2014":175.7},
{"Organisation unit":"Rangpur Division","BCG Coverage (EPI) Jan to Jun 2014":128.5,"BCG Coverage (EPI) Jul to Dec 2014":129},
{"Organisation unit":"Sylhet Division","BCG Coverage (EPI) Jan to Jun 2014":200,"BCG Coverage (EPI) Jul to Dec 2014":104.6}]
下面是我如何将输入显示到表中的代码

var _table_ = document.createElement('table'),
_tr_ = document.createElement('tr'),
_th_ = document.createElement('th'),
_td_ = document.createElement('td');

// Builds the HTML Table out of myList json data from Ivy restful service.
 function buildHtmlTable(arr) {     
     var table = _table_.cloneNode(false),
         columns = addAllColumnHeaders(arr, table);         
     for (var i=0, maxi=arr.length; i < maxi; ++i) {
         var tr = _tr_.cloneNode(false);
        for (var j=0, maxj=columns.length; j < maxj ; ++j) {
             var td = _td_.cloneNode(false);         
             cellValue = arr[i][columns[j]];                                                    
             td.appendChild(document.createTextNode(arr[i][columns[j]] || ''));            
             tr.appendChild(td);             
         }
         table.appendChild(tr);
     }
     return table;
 }

 // Adds a header row to the table and returns the set of columns.
 // Need to do union of keys from all records as some records may not contain
 // all records
 function addAllColumnHeaders(arr, table)
 {
     var columnSet = [],
         tr = _tr_.cloneNode(false);
     for (var i=0, l=arr.length; i < l; i++) {
         for (var key in arr[i]) {
             if (arr[i].hasOwnProperty(key) && columnSet.indexOf(key)===-1) {
                 columnSet.push(key);
                 var th = _th_.cloneNode(false);
                 th.appendChild(document.createTextNode(key));
                 tr.appendChild(th);
             }
         }
     }
     table.appendChild(tr);
     return columnSet;
 }
var\u table\u=document.createElement('table'),
_tr=document.createElement('tr'),
_th=document.createElement('th'),
_td=document.createElement('td');
//从Ivy restful服务的myList json数据构建HTML表。
函数buildHtmlTable(arr){
变量表=\表\克隆节点(假),
columns=AddAllColumnHeader(arr,表格);
对于(变量i=0,最大值=arr.length;i
输出结果如下所示

现在,我将介绍如何找出每行第2列和第3列之间的最小值,并将背景色设置为最小值的红色


您可以通过首先计算第2列或第3列中的最低值来完成此操作:

var smallest = Infinity;
myList.forEach(function (i) {
    smallest = Math.min(
        smallest,
        i['BCG Coverage (EPI) Jan to Jun 2014'],
        i['BCG Coverage (EPI) Jul to Dec 2014']
    );
});
然后在
buildHtmlTable
函数的最内部循环中,对照最低值检查您正在构建的
值;如果它们匹配,则在
中添加红色背景色:

for(var j=0,maxj=columns.length;j
此处的mylist是什么?实际上,我想我没有放置代码的第一部分,也没有将错误的变量作为mylist@jox谢谢你把它放在小提琴上。我在原来的帖子中错过了。谢谢@jox,但你看到结果了吗?最后一行只变成红色,但我希望每一行都变成红色row@moinkhan嗯,我想你不是每排都要。我想您希望该行中的每一列都使用它。最简单的方法是将设置颜色的行中的“td”改为“tr”。看见
for (var j=0, maxj=columns.length; j < maxj ; ++j) {
    var td = _td_.cloneNode(false);
    cellValue = arr[i][columns[j]];

    // Add red background if cellValue is the smallest value.
    if (cellValue === smallest) td.style.backgroundColor = 'red';

    td.appendChild(document.createTextNode(arr[i][columns[j]] || ''));
    tr.appendChild(td);
}