Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Maximum key.value pair from object without for loop

var obj={'one':1,'two':50,'three':75,'four':12}

This is object from which i want the output as 'three':75, this is key value pair for maximum value in object. My constraint is not to use for loop and any library. Is it possible??

like image 767
Optimus Avatar asked Aug 13 '26 20:08

Optimus


1 Answers

By the way I found another solution but it also uses loop.

var obj = {'one': 1, 'two': 50, 'three': 75, 'four': 12};

var maxKey = Object.keys(obj).sort(function (a, b) {
  return obj[a] < obj[b];
})[0];

var result = {};
result[maxKey] = obj[maxKey];
like image 167
Isabek Tashiev Avatar answered Aug 16 '26 10:08

Isabek Tashiev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!