Ваш источник данных больше не обслуживает данные. Поэтому я изменил его на место, где статистические данные, собранные Университетом Джона Хопкинса (Балтимор, Мэриленд), отражаются ежедневно. Поскольку эти данные содержат временной ряд , мне пришлось немного их предварительно обработать, прежде чем помещать в таблицу ниже. Форматирование (= раскраска) тогда лучше всего сделать несколькими строками CSS:
const num=s=>+s.replace(/[^0-9]/g,''),
cols=["country","cases","deaths","recovered","deathrate"];
fetch('https://pomber.github.io/covid19/timeseries.json').then(r=>r.json()).then(o=>{
var D=Object.keys(o).sort().map(c=>{
let dat=Object.values(o[c].slice(-1)[0]);
dat[0]=c;dat.push((100*dat[2]/dat[1]).toFixed(2)+'%');
return dat
})
document.querySelector('table').innerHTML='<tr>'+
cols.map(s=>
`<th>${s}</th>`).join('')+'</tr><tr>'
+D.map(r=>`<td>${r.join('</td><td>')}</td>`)
.join('</tr><tr>')+'</tr>';
})
th:first-child {text-align:left}
th:nth-child(n+2), td:nth-child(n+2) {text-align:right}
th:nth-child(2) {background-color: #00aa22}
th:nth-child(3) {background-color: #ffff00}
th:nth-child(4) {background-color: #ff0000}
th:nth-child(5) {background-color: #ff9900}
td:nth-child(2) {background-color: #55ee77}
td:nth-child(3) {background-color: #ffff55}
td:nth-child(4) {background-color: #ff0000}
td:nth-child(5) {background-color: #ffb84d}
<table></table>
Чисто из интереса я добавил некоторые функциональные возможности сортировки в таблицу. Нажмите на любой из заголовков столбцов, чтобы отсортировать по этому столбцу (нажмите еще раз для обратного порядка):
const num=s=>+s.replace(/[^0-9]/g,''),
cols=["country","cases","deaths","recovered","deathrate"];
const tblsort=ev=>{const e=ev.target; if (e.tagName=='TH'){
const i=[...e.parentNode.children].indexOf(e); srtdir[i]=srtdir[i]>0?-1:1;
[...tbd.children].sort(i>0?srtnum(i):srttxt(i)).forEach(tr=>tbd.appendChild(tr));
}
};
const srtdir=[];
const srtnum=i=>(a,b)=>srtdir[i]*num(a.children[i].textContent)-num(b.children[i].textContent);
const srttxt=i=>(a,b)=>srtdir[i]*a.children[i].textContent.localeCompare(b.children[i].textContent);
fetch('https://pomber.github.io/covid19/timeseries.json').then(r=>r.json()).then(o=>{
var D=Object.keys(o).sort().map(c=>{
let dat=Object.values(o[c].slice(-1)[0]);
dat[0]=c;dat.push((100*dat[2]/dat[1]).toFixed(2)+'%');
return dat
})
document.querySelector('table').innerHTML='<thead><tr>'+
cols.map(s=>
`<th>${s}</th>`).join('')+'</tr></thead><tbody><tr>'
+D.map(r=>`<td>${r.join('</td><td>')}</td>`)
.join('</tr><tr>')+'</tr></tbody>';
tbd=document.querySelector('tbody');
thd=document.querySelector('thead').addEventListener('click',tblsort);
})
th {cursor:pointer}
th:first-child {text-align:left}
th:nth-child(n+2), td:nth-child(n+2) {text-align:right}
th:nth-child(2) {background-color: #00aa22}
th:nth-child(3) {background-color: #ffff00}
th:nth-child(4) {background-color: #ff0000}
th:nth-child(5) {background-color: #ff9900}
td:nth-child(2) {background-color: #55ee77}
td:nth-child(3) {background-color: #ffff55}
td:nth-child(4) {background-color: #ff0000}
td:nth-child(5) {background-color: #ffb84d}
<table></table>