У нас есть следующие initialDataSet
0: {ID: 1, Province/State: "", Country/Region: "Thailand", Lat: 15, Long: 101, …}
1: {ID: 2, Province/State: "", Country/Region: "Japan", Lat: 36, Long: 138, …}
2: {ID: 3, Province/State: "", Country/Region: "Singapore", Lat: 1.2833, Long: 103.8333, …}
...
Внутри каждого из них у нас есть:
0:
ID: 1
Province/State: ""
Country/Region: "Thailand"
Lat: 15
Long: 101
data: (62) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {
И внутри каждого data:
У нас есть:
data: Array(62)
0: {date: "1/22/20", Confirmed: 2, Deaths: 0, Recovered: 0}
1: {date: "1/23/20", Confirmed: 3, Deaths: 0, Recovered: 0}
2: {date: "1/24/20", Confirmed: 5, Deaths: 0, Recovered: 0}
И нам нужно получить total sum of Deaths
Мы сделали:
const initialDataSet = [
{ID: 1, "Province/State": "", "Country/Region": "Thailand", Lat: 15, Long: 101, data : [ {date: "1/22/20", Confirmed: 2, Deaths: 0, Recovered: 0},
{date: "1/23/20", Confirmed: 3, Deaths: 0, Recovered: 0},
{date: "1/24/20", Confirmed: 5, Deaths: 0, Recovered: 0}
]},
{ID: 2, "Province/State": "", "Country/Region": "Japan", Lat: 36, Long: 138, data : [ {date: "1/22/20", Confirmed: 2, Deaths: 0, Recovered: 0},
{date: "1/23/20", Confirmed: 3, Deaths: 0, Recovered: 0},
{date: "1/24/20", Confirmed: 5, Deaths: 0, Recovered: 0}
]},
{ID: 3, "Province/State": "", "Country/Region": "Singapore", Lat: 1.2833, Long: 103.8333, data: [ {date: "1/22/20", Confirmed: 2, Deaths: 0, Recovered: 0},
{date: "1/23/20", Confirmed: 3, Deaths: 0, Recovered: 0},
{date: "1/24/20", Confirmed: 5, Deaths: 0, Recovered: 0}
]}
];
var testTotals = [];
for (var d = 0; d < initialDataSet.length; d++) {
var trackObj = initialDataSet[d];
for (var c = 0; c < trackObj.data.length; c++) {
testTotals.push(parseInt(trackObj.data[c].Deaths));
}
}
var myTots = testTotals.reduce((a, b) => a + b, 0);
console.log(myTots);
Но консоль дает NaN