使用javascript找到最低值

时间:2015-07-09 17:45:16

标签: 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 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;
 }

输出看起来像这样

enter image description here

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

here is the code

1 个答案:

答案 0 :(得分:1)

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

#read in the file
fileToRead = open('strings.txt')

d = {}
#iterate over every line in the file
for line in fileToRead:
    listOfWords = line.split()
    #iterate over every word in the list
    for word in listOfWords:
        if word not in d:
            d[word] = 1
        else:
            d[word] = d.get(word) + 1
#sort the keys
listF = sorted(d)

#iterate over sorted keys and write them in the file with appropriate value
with open('count.txt', 'a') as fileToWrite:
    for word in listF:
        string = "{:<18}\t\t\t{}\n".format(word, d.get(word))
        print string,
        fileToWrite.write(string)

然后在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的值;如果匹配,请向<td>添加红色背景颜色:

<td>