How can I find the maximum value among multiple keys in an array?
I previously attempted to find the maximum values for just three keys.
getMaxValuefromkeys(values: any[], key1: string, key2: string, key3: string) {
var val1 = Math.max.apply(Math, values.map(function (a) { return a[key1] }));
var val2 = Math.max.apply(Math, values.map(function (a) { return a[key2]; }));
var val3 = Math.max.apply(Math, values.map(function (a) { return a[key2]; }));
if (val1 >= val2 || val1 >= val3) {
return val1;
} else if (val2 >= val3 || val2 >= val1) {
return val2;
}
return val3;
}
However, I realized that using this approach requires more conditions and code when dealing with multiple keys. Thus, I explored the following solution:
Math.max.apply(Math, values.map(function (a) { return a[key1], a[key2], a[key3]; }));
// here I attempt to use multiple keys
Unfortunately, this approach did not work as expected. Is there a single line of code available for finding the maximum value among multiple keys in an array?