Имея массив 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 для day: 1
и city: "New York"
, и я хочу получить max: 22
и 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, которые мне нужны для получения желаемого результата. Спасибо!