尝试为我想要的聚合解析 2-d JSON 数组
Trying to parse 2-d JSON array for the aggregations I want
基本上我想解析这个 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"}
您可以使用 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;}
基本上我想解析这个 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"}
您可以使用 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;}