告诉我 Math.max 中哪个值最高

Tell me which value from Math.max was the highest

我正在使用以下代码 return a、b、c 中的最高值。

它这样做很成功,但是除了 return 最高值(这是一个数字)之外,我还想告诉我哪个变量 return 最高值。例如,变量“c”具有最高值。

let a = 1
let b = 2
let c = 3

let maxReturn = Math.max(a, b, c);
console.log(maxReturn);

这将 return 3 作为结果,这是我需要的,但我如何也输出具有最高值的实际变量?在这种情况下告诉我:“最高值为 3,在 C 中找到”

谢谢!!

请参阅第二个示例以获得更好的方法。记住索引是基于 0

let a = 1
let b = 2
let c = 3

let maxReturn = Math.max(a, b, c);
if (maxReturn == 1)console.log(maxReturn,"a");
else if(maxReturn == 2)console.log(maxReturn,"b");
else if(maxReturn == 3)console.log(maxReturn,"c");



let x = [1,2,3]

maxReturn= Math.max(...x)
let index = x.indexOf(maxReturn)
console.log(maxReturn,index)

对于变量,如果没有检查变量并查看其是否匹配的编码语句,很难知道什么变量与 a、b、c 对齐。

使用可以循环的格式意味着您可以轻松引用。

const getMax = (data) => Object.entries(data).reduce((max, item) => max[1] > item[1] ? max : item);

var myData1 = { a: 1, b:2, c: 3};
var myData2 = { a: 4, b:2, c: 3};

console.log(getMax(myData1));
console.log(getMax(myData2));