查找对象数组中属性的最大值 [英] Finding the max value of an attribute in an array of objects

查看:655
本文介绍了查找对象数组中属性的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种非常快速,干净且高效的方法来获取以下JSON切片中的最大y值:

I'm looking for a really quick, clean and efficient way to get the max "y" value in the following JSON slice:

[{"x":"8/11/2009","y":0.026572007},{"x":"8/12/2009","y":0.025057454},{"x":"8/13/2009","y":0.024530916},{"x":"8/14/2009","y":0.031004457}]

for循环是唯一可行的方法吗?我非常喜欢使用 Math.max

Is a for-loop the only way to go about it? I'm keen on somehow using Math.max.

推荐答案

找到数组中对象的最大 y 值:

Math.max.apply(Math, array.map(function(o) { return o.y; }))

这篇关于查找对象数组中属性的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆