js中找出对象中值最大的一项

发布时间 2023-05-09 09:20:38作者: ZerlinM

比如有以下对象

const obj = {
  num1: 1000,
  num2: 800,
  num3: 900,
}

期望得到 num1: 1000这一项。

js实现代码:

export const maxIncome = (userWalletIncomes) => {
  let maxValue = 0;
  let maxKey = '';
  for (const [key, value] of Object.entries(userWalletIncomes)) {
    if(value > maxValue) {
      maxValue = value;
      maxKey = key
    }
  }
  return `${maxKey} : ${maxValue}`
}