我有一个看起来像这样的对象:
const yo = {
one: {
value: 0,
mission: 17},
two: {
value: 18,
mission: 3},
three: {
value: -2,
mission: 4},
}
我想在嵌套对象中找到 mission
属性的最小值。此行用于查找嵌套 value
Prop 的最小值并返回 -2
:
const total = Object.values(yo).reduce((t, {value}) => Math.min(t, value), 0)
但是当我为 mission
Prop 尝试同样的事情时,它返回 0
而它应该是 3
:
const total = Object.values(yo).reduce((t, {mission}) => Math.min(t, mission), 0)
我有什么遗漏或做错了吗?
最佳答案
在这种情况下,map
就足够了。
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));
关于javascript - 查找嵌套对象属性的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64962678/