计算一些基本操作并使用 javascript 转换 json 数组

Calculate some basic operations and transform a json array using javascript

具有以下格式的 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}
]

如何使用 Javascript 按城市和日期计算最大值和最小值 value

例如,对于 day: 1city: "New York",我有以下 values 16、22 和 20,我想得到 max: 22min: 16.一旦计算出来,我想将这些新值附加到所有对象,无论 year 包含 city: "New York"

day: 1
[
  {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 循环结构方面遇到问题。谢谢!

您可以使用 array.reduce to find min-max pairs for every (day,city) pair and then run 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);

希望对您有所帮助。

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);