How to display the maximum and minimum value of an object?

  • 0
    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

  • 2 Answers
  • 0
    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

  • 0
    Object.values ​​(Dnevnik) .sort ();



    And from this array, take the first and last value.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!