This should be an easy one. I just cant figure it out.

How do I get the largest value from this piece of JSON with javascript.

{"data":{"one":21,"two":35,"three":24,"four":2,"five":18},"meta":{"title":"Happy with the service"}}

The key and value I need is:

"two":35 

as it is the highest

thanks

share|improve this question
var jsonText = '{"data":{"one":21,"two":35,"three":24,"four":2,"five":18},"meta":{"title":"Happy with the service"}}'
var data = JSON.parse(jsonText).data
var maxProp = null
var maxValue = -1
for (var prop in data) {
  if (data.hasOwnProperty(prop)) {
    var value = data[prop]
    if (value > maxValue) {
      maxProp = prop
      maxValue = value
    }
  }
}
share|improve this answer
    
Using hasOwnProperty() is a good point. – Álvaro González Apr 12 '10 at 9:41
    
@Insin What does hasOwnProperty mean?Why you used eval? – systempuntoout Apr 12 '10 at 9:44
2  
@systempuntoout hasOwnProperty guards against naughty libraries adding things to Object.prototype, as we don't know the full context in which this code will be executed. I used eval() as the question was asked with regard to JSON - JSON is a text format, so always takes the form of strings which conform to the specification at json.org. It could be that the question asker was confusing JSON with Object Literal Notation (there are many, many misleading tutorials/articles out there which don't help with that), which I why I made a point of using a JSON text. – Jonny Buchanan Apr 12 '10 at 10:35
    
+1 for the clear explanation! – systempuntoout Apr 12 '10 at 10:40
1  
Note this only works if largest property is larger than -1. – Michael Anderson Aug 15 '13 at 2:40

If you have underscore:

var max_key = _.invert(data)[_.max(data)];

How this works:

var data = {one:21, two:35, three:24, four:2, five:18};
var inverted = _.invert(data); // {21:'one', 35:'two', 24:'three', 2:'four', 18:'five'};
var max = _.max(data); // 35
var max_key = inverted[max]; // {21:'one', 35:'two', 24:'three', 2:'four', 18:'five'}[35] => 'two'
share|improve this answer

This is my function for biggest key

function maxKey(a) {  
  var max, k; // don't set max=0, because keys may have values < 0  
  for (var key in a) { if (a.hasOwnProperty(key)) { max = parseInt(key); break; }} //get any key  
  for (var key in a) { if (a.hasOwnProperty(key)) { if((k = parseInt(key)) > max) max = k; }}  
  return max;  
} 
share|improve this answer
    
+1 for being the only solution that properly handles negative values properly. – Michael Anderson Aug 15 '13 at 2:41

You can also iterate the object after you parse JSON .

var arr = jQuery.parseJSON('{"one":21,"two":35,"three":24,"four":2,"five":18}' );

var maxValue = 0;

for (key in arr)
{
     if (arr[key] > maxValue)
     {
          maxValue = arr[key];   
     }
}

console.log(maxValue);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.