get the property with the largest number - Javascript Data Type

Javascript examples for Data Type:Number

Description

get the property with the largest number

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(){/*  w  w  w  . j a  va 2 s . c  om*/
var obj = {2:1, 3:4, a:8, 5:2, 3:5, 3:5};
var max = -1;
for(var propertyName in obj) {
    if(!isNaN(propertyName) && propertyName > 0){
        if(obj.hasOwnProperty(propertyName) && obj[propertyName] > max){
            max = obj[propertyName];
        }
    }
}
console.log(max);
    }

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

Related Tutorials