gpt4 book ai didi

javascript - 如何获得数组中正确的数组和?

转载 作者:行者123 更新时间:2023-11-28 12:51:46 24 4
gpt4 key购买 nike

我们有以下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: 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}

我们需要获取死亡总数

我们做到了:

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

最佳答案

鉴于(大)data set您正在处理的内容,可以提取以下额外信息:

  1. 时间序列是增量的,这意味着data数组的最后一个值是该时间点的总数;这减少了一个内部循环
  2. 如果 data 数组的最后一个值是空字符串,则其前面没有正整数,这意味着您可以使用 || 强制该值0

我调整了您的数据集以突出显示这些发现,并相应地更改了解决方案。

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: '', 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: 2, Recovered: 0},
{date: "1/24/20", Confirmed: 5, Deaths: 3, 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: '', Recovered: 0}
]}
];

const x = initialDataSet.reduce((total, {data}) => {
const lastItem = data[data.length - 1]
// it's unlikely, but possible that `data` is an empty array
return lastItem ? total + (lastItem.Deaths || 0) : total
}, 0)
console.log(x);

关于javascript - 如何获得数组中正确的数组和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60832878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com