Getting variable name with highest value using array sort and custom function - Javascript Array

Javascript examples for Array:sort

Description

Getting variable name with highest value using array sort and custom function

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from w w  w .  j  a  va 2s .  com*/
var _myDemoArray = [
{name:'Gain_weight',
 value:0.01},
{name:'Gain_weather',
value:0.02}]
var _getSortedElem = _myDemoArray.sort(function(a,b){
  return b.value -a.value;
});
console.log(_getSortedElem[0].name);
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials