Рассчитать общее количество значений из того же ключа JSON - PullRequest
0 голосов
/ 11 апреля 2020

Я хочу рассчитать общее количество случаев, выздоровлений и смертей в целом от объекта JSON. Предположим, к примеру: дата 3/27/20 должна иметь значение вычисленного общего числа выздоровлений и случаев смерти для остальных стран. Как я могу сделать в этом JavaScript и вернуть мне объект вычисленных значений для каждой даты, удалив страны, провинции? У меня есть JSON объект:

{
"country": "Afghanistan",
"province": null,
"timeline": {
"cases": {
"3/27/20": 110,
"3/28/20": 110,
"3/29/20": 120,
"3/30/20": 170,
"3/31/20": 174,
"4/1/20": 237,
"4/2/20": 273,
"4/3/20": 281,
"4/4/20": 299,
"4/5/20": 349,
"4/6/20": 367,
"4/7/20": 423,
"4/8/20": 444,
"4/9/20": 484,
"4/10/20": 521
},
"deaths": {
"3/27/20": 4,
"3/28/20": 4,
"3/29/20": 4,
"3/30/20": 4,
"3/31/20": 4,
"4/1/20": 4,
"4/2/20": 6,
"4/3/20": 6,
"4/4/20": 7,
"4/5/20": 7,
"4/6/20": 11,
"4/7/20": 14,
"4/8/20": 14,
"4/9/20": 15,
"4/10/20": 15
},
"recovered": {
"3/27/20": 2,
"3/28/20": 2,
"3/29/20": 2,
"3/30/20": 2,
"3/31/20": 5,
"4/1/20": 5,
"4/2/20": 10,
"4/3/20": 10,
"4/4/20": 10,
"4/5/20": 15,
"4/6/20": 18,
"4/7/20": 18,
"4/8/20": 29,
"4/9/20": 32,
"4/10/20": 32
}
}
},
{
"country": "Albania",
"province": null,
"timeline": {
"cases": {
"3/27/20": 186,
"3/28/20": 197,
"3/29/20": 212,
"3/30/20": 223,
"3/31/20": 243,
"4/1/20": 259,
"4/2/20": 277,
"4/3/20": 304,
"4/4/20": 333,
"4/5/20": 361,
"4/6/20": 377,
"4/7/20": 383,
"4/8/20": 400,
"4/9/20": 409,
"4/10/20": 416
},
"deaths": {
"3/27/20": 8,
"3/28/20": 10,
"3/29/20": 10,
"3/30/20": 11,
"3/31/20": 15,
"4/1/20": 15,
"4/2/20": 16,
"4/3/20": 17,
"4/4/20": 20,
"4/5/20": 20,
"4/6/20": 21,
"4/7/20": 22,
"4/8/20": 22,
"4/9/20": 23,
"4/10/20": 23
},
"recovered": {
"3/27/20": 31,
"3/28/20": 31,
"3/29/20": 33,
"3/30/20": 44,
"3/31/20": 52,
"4/1/20": 67,
"4/2/20": 76,
"4/3/20": 89,
"4/4/20": 99,
"4/5/20": 104,
"4/6/20": 116,
"4/7/20": 131,
"4/8/20": 154,
"4/9/20": 165,
"4/10/20": 182
}
}
}

1 Ответ

0 голосов
/ 11 апреля 2020

var a = [{"country":"Afghanistan","province":null,"timeline":{"cases":{"3/27/20":110,"3/28/20":110,"3/29/20":120,"3/30/20":170,"3/31/20":174,"4/1/20":237,"4/2/20":273,"4/3/20":281,"4/4/20":299,"4/5/20":349,"4/6/20":367,"4/7/20":423,"4/8/20":444,"4/9/20":484,"4/10/20":521},"deaths":{"3/27/20":4,"3/28/20":4,"3/29/20":4,"3/30/20":4,"3/31/20":4,"4/1/20":4,"4/2/20":6,"4/3/20":6,"4/4/20":7,"4/5/20":7,"4/6/20":11,"4/7/20":14,"4/8/20":14,"4/9/20":15,"4/10/20":15},"recovered":{"3/27/20":2,"3/28/20":2,"3/29/20":2,"3/30/20":2,"3/31/20":5,"4/1/20":5,"4/2/20":10,"4/3/20":10,"4/4/20":10,"4/5/20":15,"4/6/20":18,"4/7/20":18,"4/8/20":29,"4/9/20":32,"4/10/20":32}}},{"country":"Albania","province":null,"timeline":{"cases":{"3/27/20":186,"3/28/20":197,"3/29/20":212,"3/30/20":223,"3/31/20":243,"4/1/20":259,"4/2/20":277,"4/3/20":304,"4/4/20":333,"4/5/20":361,"4/6/20":377,"4/7/20":383,"4/8/20":400,"4/9/20":409,"4/10/20":416},"deaths":{"3/27/20":8,"3/28/20":10,"3/29/20":10,"3/30/20":11,"3/31/20":15,"4/1/20":15,"4/2/20":16,"4/3/20":17,"4/4/20":20,"4/5/20":20,"4/6/20":21,"4/7/20":22,"4/8/20":22,"4/9/20":23,"4/10/20":23},"recovered":{"3/27/20":31,"3/28/20":31,"3/29/20":33,"3/30/20":44,"3/31/20":52,"4/1/20":67,"4/2/20":76,"4/3/20":89,"4/4/20":99,"4/5/20":104,"4/6/20":116,"4/7/20":131,"4/8/20":154,"4/9/20":165,"4/10/20":182}}}]

var result = a.map(function(e){return {cases: e.timeline.cases, deaths: e.timeline.deaths, recovered: e.timeline.recovered};}).reduce(function(acc, e){
    
    Object.keys(e).forEach(function(t){
       Object.keys(e[t]).forEach(function(d){
            acc[t][d] = (acc[t][d] || 0) + e[t][d];
       });
    });
    return acc;
}, {deaths: {}, recovered: {}, cases: {}});

console.log(result);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...