使用Javascript从Json对象获取最大值 [英] Get the largest value from Json object with Javascript
本文介绍了使用Javascript从Json对象获取最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这应该很简单。我只是想不通。
This should be an easy one. I just cant figure it out.
如何使用javascript从这段JSON中获取最大值。
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
因为它是最高的
谢谢
推荐答案
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
}
}
}
这篇关于使用Javascript从Json对象获取最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文