If I have a weather data, which is an array, like this one below.
const weather = [
{date:'2022-07-18', min_temp:15, max_temp:26, type:'clouds'},
{date:'2022-07-18', min_temp:13, max_temp:27, type:'sun'},
{date:'2022-07-18', min_temp:23, max_temp:31, type:'clouds'},
{date:'2022-07-18', min_temp:19, max_temp:28, type:'rain'},
{date:'2022-07-19', min_temp:21, max_temp:25, type:'rain'},
{date:'2022-07-19', min_temp:17, max_temp:29, type:'rain'},
{date:'2022-07-19', min_temp:14, max_temp:38, type:'rain'},
{date:'2022-07-19', min_temp:9, max_temp:30, type:'clouds'}
]
How can I reduce the array, one data/date, with the min and max temperature from the same date, and type should be the most frequently type from the same date. It should look like this one below.
const weather = [
{date:'2022-07-18', min_temp=13, max_temp31, type:'clouds'},
{date:'2022-07-19', min_temp=9, max_temp:38, type:'rain'}
]
CodePudding user response:
You can use reduce
to collect the values per date into a map, and then get the values of that map:
const weather = [
{date:'2022-07-18', min_temp:15, max_temp:26, type:'clouds'},
{date:'2022-07-18', min_temp:13, max_temp:27, type:'sun'},
{date:'2022-07-18', min_temp:23, max_temp:31, type:'clouds'},
{date:'2022-07-18', min_temp:19, max_temp:28, type:'rain'},
{date:'2022-07-19', min_temp:21, max_temp:25, type:'rain'},
{date:'2022-07-19', min_temp:17, max_temp:29, type:'rain'},
{date:'2022-07-19', min_temp:14, max_temp:38, type:'rain'},
{date:'2022-07-19', min_temp:9, max_temp:30, type:'clouds'}
]
const weatherByDate = weather.reduce((acc, curr) => {
const date = curr.date
const weather = acc[date]
if (weather) {
const copy = { ...weather }
copy.min_temp = Math.min(curr.min_temp, weather.min_temp)
copy.max_temp = Math.max(curr.max_temp, weather.max_temp)
const count = (weather.typeCounts[curr.type] || 0) 1
copy.typeCounts[curr.type] = count
if(count > weather.typeCounts.max) {
copy.typeCounts.max = count
copy.type = curr.type
}
acc[date] = copy
} else {
acc[date] = { ...curr, typeCounts: { [curr.type]: 1, max: 1 } }
}
return acc
}, {})
const arr = Object.values(weatherByDate).map(({typeCounts, ...rest}) => rest)
console.log(arr)
EDIT: Edited to keep most frequent type
per date
CodePudding user response:
The idea is first to group by date. This is done using the first reduce. While we do, we store the frequency of each type. and the min/max temp.
Then we take the values of this object as the required array. Only a little housekeeping removing the freq
helper object. And setting the type
to be indeed the most frequent (amongst that date).
const weather = [
{date:'2022-07-18', min_temp:15, max_temp:26, type:'clouds'},
{date:'2022-07-18', min_temp:13, max_temp:27, type:'sun'},
{date:'2022-07-18', min_temp:23, max_temp:31, type:'clouds'},
{date:'2022-07-18', min_temp:19, max_temp:28, type:'rain'},
{date:'2022-07-19', min_temp:21, max_temp:25, type:'rain'},
{date:'2022-07-19', min_temp:17, max_temp:29, type:'rain'},
{date:'2022-07-19', min_temp:14, max_temp:38, type:'rain'},
{date:'2022-07-19', min_temp:9, max_temp:30, type:'clouds'}
];
var step1 = Object.values(weather.reduce(function(agg, item) {
var {date, min_temp, max_temp, type} = item;
if (agg[date]) {
agg[date].min_temp = Math.min(agg[date].min_temp, min_temp)
agg[date].max_temp = Math.max(agg[date].max_temp, max_temp)
} else {
agg[date] = item
agg[date].freq = {}
}
agg[date].freq[type] = (agg[date].freq[type] || 0) 1
return agg;
}, {})).map(function(item) {
var most_common = 0;
Object.entries(item.freq).reduce(function(agg, [key, value]) {
if (agg<value) {
agg = value;
most_common = key;
}
}, 0);
delete item.freq;
item.type = most_common;
return item;
})
console.log(step1)