How to display the maximum and minimum value of an object?
-
I have an object
let Dnevnik = {math: 65, philo: 85, physics: 60};
I need to display the maximum and minimum values. I tried to do it somehow, but the code didn't find such values.
Here's the code itself:
Math.max.apply(Math, Courses.map(function(o) { return o; }))
JavaScript Anonymous, Jul 13, 2019 -
const values = Object.values(obj);
const min = Math.min(...values);
const max = Math.max(...values);
or
const { min, max } = Object
.values(obj)
.reduce(({ min, max }, n) => ({
min: n < min ? n : min,
max: n > max ? n : max,
}), { min: Infinity, max: -Infinity });Anonymous -
Object.values (Dnevnik) .sort ();
And from this array, take the first and last value.Anonymous
2 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!