Skip to content Skip to sidebar Skip to footer

Finding The Minimum Value Of A Nested Object Property

I have an object that looks like this: const yo = { one: { value: 0, mission: 17}, two: { value: 18, mission: 3}, three: { value: -2, mission: 4}, }

Solution 1:

In this case, map is enough.

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

const total = Object.values(yo).map(({ mission }) => mission);
console.log(Math.min(...total));

Solution 2:

You are passing 0 as the initial value of accumulator i.e t. 0 is less than all the mission values. So you need to pass the greatest value i.e Infinity as second argument of reduce().

const yo = {
  one: {
    value: 0,
    mission: 17},
  two: {
    value: 18,
    mission: 3},
  three: {
    value: -2,
    mission: 4},
}
const total = Object.values(yo).reduce((t, {mission}) =>Math.min(t, mission), Infinity);
console.log(total)

Post a Comment for "Finding The Minimum Value Of A Nested Object Property"