Вычислить несколько базовых c операций и преобразовать массив json, используя javascript - PullRequest
1 голос
/ 07 мая 2020

Имея массив 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, которые мне нужны для получения желаемого результата. Спасибо!

Ответы [ 2 ]

1 голос
/ 07 мая 2020

Вы можете использовать array.reduce, чтобы найти пары min-max для каждой пары (day,city), а затем запустить array.map , чтобы объединить агрегаты с вашим массивом:

let input = [
  {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}
];

let keyFn = obj => `${obj.city}-${obj.day}`;

let minMax = input.reduce((acc,cur)=> {
   let key = keyFn(cur);
   let prev = acc[key];
   if(!prev){
      acc[key] = { min: cur.value, max: cur.value };
   } else {
      prev.min = Math.min(prev.min, cur.value);
      prev.max = Math.max(prev.max, cur.value);
   }
   return acc;
}, {});


let result = input.map(obj => ({...obj, ...minMax[keyFn(obj)]}));
console.log(result);
0 голосов
/ 07 мая 2020

Надеюсь, это вам поможет.

data =[
  {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}
]
//filter input value
var cityParam = 'New York';
var dayParam = 2;


const maxValue = data.reduce(
  (max, data) => (data.value > max  && data.city === cityParam && data.day ===dayParam ? data.value : max),
  data[0].value
);
const minValue = data.reduce(
  (max, data) => (data.value < max  && data.city === cityParam && data.day ===dayParam ? data.value : max),
  data[0].value
);
console.log(maxValue);
console.log(minValue);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...