Page 1 of 1

Finding the minimum value of a nested object property

Posted: 23 Nov 2020, 07:31
by Marius
I have a nested object in JavaScript that looks like this:

Code: Select all

const yo = {
  one: {
    value: 0,
    mission: 17},
  two: {
    value: 18,
    mission: 3},
  three: {
    value: -2,
    mission: 4},
}
I want to find the minimum value of the 'mission' property within the nested objects.
How can I do that in a simple way?

Finding the minimum value of a nested object property

Posted: 23 Nov 2020, 08:53
by Admin
Try combine the Object.values() and map() methods, like in the following example:

Code: Select all

const yo = {
  one: {
    value: 9,
    mission: 17
  },
  two: {
    value: 18,
    mission: 6
  },
  three: {
    value: 3,
    mission: 4
  },
}

const val_mission = Object.values(yo).map(({ mission }) => mission);
let min_mission = Math.min(...val_mission);

console.log(min_mission);  // 4