具有这种格式的 json 数组:
[
{year: 2020, day: 1, city: "New York", value: 16}
{year: 2020, day: 2, city: "New York", value: 12}
{year: 2020, day: 3, city: "New York", value: 10}
{year: 2019, day: 1, city: "New York", value: 22}
{year: 2019, day: 2, city: "New York", value: 18}
{year: 2019, day: 3, city: "New York", value: 14}
{year: 2018, day: 1, city: "New York", value: 20}
{year: 2018, day: 2, city: "New York", value: 8}
{year: 2018, day: 3, city: "New York", value: 14}
{year: 2020, day: 1, city: "Boston", value: 11}
{year: 2020, day: 2, city: "Boston", value: 17}
{year: 2020, day: 3, city: "Boston", value: 19}
{year: 2019, day: 1, city: "Boston", value: 21}
{year: 2019, day: 2, city: "Boston", value: 9}
{year: 2019, day: 3, city: "Boston", value: 11}
{year: 2018, day: 1, city: "Boston", value: 7}
{year: 2018, day: 2, city: "Boston", value: 23}
{year: 2018, day: 3, city: "Boston", value: 19}
]
如何value
使用 Javascript 按城市和日期计算最大值和最小值?
例如,我有以下value
s 16、22 和 20 for day: 1
andcity: "New York"
我想得到max: 22
and min: 16
。一旦计算出来,我想将这些新值附加到所有对象,无论year
, 包含day: 1
什么city: "New York"
[
{year: 2020, day: 1, city: "New York", value: 16, max: 22, min: 16}
{year: 2020, day: 2, city: "New York", value: 12, max: 18, min: 8}
{year: 2020, day: 3, city: "New York", value: 10, max: 14, min: 10}
{year: 2019, day: 1, city: "New York", value: 22, max: 22, min: 16}
{year: 2019, day: 2, city: "New York", value: 18, max: 18, min: 8}
{year: 2019, day: 3, city: "New York", value: 14, max: 14, min: 10}
{year: 2018, day: 1, city: "New York", value: 20, max: 22, min: 16}
{year: 2018, day: 2, city: "New York", value: 8, max: 18, min: 8}
{year: 2018, day: 3, city: "New York", value: 14, max: 14, min: 10}
{year: 2020, day: 1, city: "Boston", value: 11, max: 21, min: 7}
{year: 2020, day: 2, city: "Boston", value: 17, max: 23, min: 9}
{year: 2020, day: 3, city: "Boston", value: 19, max: 19, min: 11}
{year: 2019, day: 1, city: "Boston", value: 21, max: 21, min: 7}
{year: 2019, day: 2, city: "Boston", value: 9, max: 23, min: 9}
{year: 2019, day: 3, city: "Boston", value: 11, max: 19, min:11}
{year: 2018, day: 1, city: "Boston", value: 7, max: 21, min: 7}
{year: 2018, day: 2, city: "Boston", value: 23, max: 23, min: 9}
{year: 2018, day: 3, city: "Boston", value: 19, max: 21, min: 7}
]
计算最大值或最小值不是问题。我在获取所需结果所需的 for 循环结构方面遇到问题。谢谢!