使用Javascript从Json对象获取最大值

时间:2022-05-04 07:40:58

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.

如何使用javascript从这段JSON中获取最大值。

{"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

谢谢

4 个解决方案

#1


9  

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
    }
  }
}

#2


8  

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'

#3


1  

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;  
} 

#4


0  

You can also iterate the object after you parse JSON .

您还可以在解析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);

#1


9  

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
    }
  }
}

#2


8  

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'

#3


1  

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;  
} 

#4


0  

You can also iterate the object after you parse JSON .

您还可以在解析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);