In my experiment, I have 3 distinct scenarios which are fed into the function findMinDateAndMaxValue
:
{}
{ '2022-04-29': 1 }
{ '2022-04-29': 3, '2022-04-30': 2, '2022-04-28': 3, '2022-05-02': 2 }
My objective is to extract the key:value pair with the earliest date and the highest value from each scenario. However, in the third case, I encounter an issue where I get '2022-04-29': 3
instead of '2022-04-28': 3
findMinDateAndMaxValue = dates => {
return Object.keys(dates).reduce((prev, curr) => {
if (dates[curr] > prev.date) {
return {
val: dates[curr],
date: curr
};
} else {
return prev;
}
}, {
val: 0,
date: null
}); }
expected outcome for scenario 1 : { val: 0, date: null }
expected outcome for scenario 2 : { val: 1, date: '2022-04-29' }
expected outcome for scenario 3: { val: 3, date: '2022-04-28' }