【发布时间】:2021-07-26 03:24:09
【问题描述】:
我有一个带有多个键的对象(例如 idOne、idTwo、idThree、idFour)……每个键都包含一个对象数组。我想以最低价格返回并输出密钥。在本例中,idThree 包含 id 的最低价格,因此应输出 idThree。我有返回找到的最低价格的代码......但我的目标是返回密钥(idThree)。有没有更简单/更干净的方法?
const object = {
idOne: [{ price: 300 }],
idTwo: [{ price: 200 }, { price: 100 }],
idThree: [{ price: 90 }, { price: 100 }],
idFour: [{ price: 99 }, { price: 210 }]
}
当前代码
const arrayOfMinValues = []
for (const [key, value] of Object.entries(object)) {
const minimumEntry = Math.min(...value.map(item => item.price))
arrayOfMinValues.push(minimumEntry)
}
console.log('MIN VALUE IS: ', Math.min(...arrayOfMinValues)) // how can I return key?
【问题讨论】:
标签: javascript arrays json object ecmascript-6