尝试解析二维JSON数组以获取所需的聚合

问题描述 投票:0回答:1

基本上,我想解析此JSON 2天数组(下面的快照数据示例),以总结每个日期的病例和死亡的所有值,包括状态。它将忽略“状态”列,而仅将每个日期的所有州的病例和死亡总数相加。我可以用SQL做到,用javascript很难。

所以最终它将看起来像:

JSON二维数组,如下所示:

{date: "2020-02-17", totalCases: "7", totalDeaths: "1"}
{date: "2020-02-18", totalCases: "150", totalDeaths: "2"}
{date: "2020-02-19",  totalCases: "210", totalDeaths: "3"}
{date: "2020-02-20",  totalCases: "350", totalDeaths: "7"}

要汇总的原始JSON对象在下面

138: {date: "2020-02-18", state: "California", fips: "06", cases: "7", deaths: "0"}
139: {date: "2020-02-18", state: "Illinois", fips: "17", cases: "2", deaths: "0"}
140: {date: "2020-02-18", state: "Massachusetts", fips: "25", cases: "1", deaths: "0"}
141: {date: "2020-02-18", state: "Nebraska", fips: "31", cases: "10", deaths: "0"}
142: {date: "2020-02-18", state: "Texas", fips: "48", cases: "2", deaths: "0"}
143: {date: "2020-02-18", state: "Washington", fips: "53", cases: "1", deaths: "0"}
144: {date: "2020-02-18", state: "Wisconsin", fips: "55", cases: "1", deaths: "0"}
145: {date: "2020-02-19", state: "Arizona", fips: "04", cases: "1", deaths: "0"}
146: {date: "2020-02-19", state: "California", fips: "06", cases: "7", deaths: "0"}
147: {date: "2020-02-19", state: "Illinois", fips: "17", cases: "2", deaths: "0"}
148: {date: "2020-02-19", state: "Massachusetts", fips: "25", cases: "1", deaths: "0"}
149: {date: "2020-02-19", state: "Nebraska", fips: "31", cases: "10", deaths: "0"}
150: {date: "2020-02-19", state: "Texas", fips: "48", cases: "2", deaths: "0"}
151: {date: "2020-02-19", state: "Washington", fips: "53", cases: "1", deaths: "0"}
152: {date: "2020-02-19", state: "Wisconsin", fips: "55", cases: "1", deaths: "0"}
153: {date: "2020-02-20", state: "Arizona", fips: "04", cases: "1", deaths: "0"}
154: {date: "2020-02-20", state: "California", fips: "06", cases: "8", deaths: "0"}
155: {date: "2020-02-20", state: "Illinois", fips: "17", cases: "2", deaths: "0"}
156: {date: "2020-02-20", state: "Massachusetts", fips: "25", cases: "1", deaths: "0"}
157: {date: "2020-02-20", state: "Nebraska", fips: "31", cases: "11", deaths: "0"}
javascript json parsing multidimensional-array aggregation
1个回答
0
投票

您可以使用reduce来完成。

const records = {
	138: {date: "2020-02-18", state: "California", fips: "06", cases: "7", deaths: "2"},
	139: {date: "2020-02-18", state: "Illinois", fips: "17", cases: "2", deaths: "0"},
	140: {date: "2020-02-18", state: "Massachusetts", fips: "25", cases: "1", deaths: "0"},
	141: {date: "2020-02-18", state: "Nebraska", fips: "31", cases: "10", deaths: "0"},
	142: {date: "2020-02-18", state: "Texas", fips: "48", cases: "2", deaths: "0"},
	143: {date: "2020-02-18", state: "Washington", fips: "53", cases: "1", deaths: "2"},
	144: {date: "2020-02-18", state: "Wisconsin", fips: "55", cases: "1", deaths: "0"},
	145: {date: "2020-02-19", state: "Arizona", fips: "04", cases: "1", deaths: "0"},
	146: {date: "2020-02-19", state: "California", fips: "06", cases: "7", deaths: "0"},
	147: {date: "2020-02-19", state: "Illinois", fips: "17", cases: "2", deaths: "0"},
	148: {date: "2020-02-19", state: "Massachusetts", fips: "25", cases: "1", deaths: "0"},
	149: {date: "2020-02-19", state: "Nebraska", fips: "31", cases: "10", deaths: "0"},
	150: {date: "2020-02-19", state: "Texas", fips: "48", cases: "2", deaths: "0"},
	151: {date: "2020-02-19", state: "Washington", fips: "53", cases: "1", deaths: "0"},
	152: {date: "2020-02-19", state: "Wisconsin", fips: "55", cases: "1", deaths: "0"},
	153: {date: "2020-02-20", state: "Arizona", fips: "04", cases: "1", deaths: "0"},
	154: {date: "2020-02-20", state: "California", fips: "06", cases: "8", deaths: "0"},
	155: {date: "2020-02-20", state: "Illinois", fips: "17", cases: "2", deaths: "0"},
	156: {date: "2020-02-20", state: "Massachusetts", fips: "25", cases: "1", deaths: "0"},
	157: {date: "2020-02-20", state: "Nebraska", fips: "31", cases: "11", deaths: "0"}
};


const res = Object.values(records).reduce((acc, {date, cases, deaths}) => {
	if (acc[date] !== undefined) {
		acc[date].totalCases += (+cases);
		acc[date].totalDeaths += (+deaths);
	} else {
		acc[date] = {date, totalCases: (+cases), totalDeaths: (+deaths)};
	}

	return acc;
}, {});

console.log(res);
.as-console-wrapper {min-height: 100% !important; top: 0;}
© www.soinside.com 2019 - 2024. All rights reserved.