查找对象数组中某属性的最大最小值

var array=[
        {
            "index_id": 119,
            "area_id": "18335623",
            "name": "满意度",
            "value": "100"
        },
        {
            "index_id": 119,
            "area_id": "18335624",
            "name": "满意度",
            "value": "20"
        },
        {
            "index_id": 119,
            "area_id": "18335625",
            "name": "满意度",
            "value": "80"
        }];

 
一行代码搞定

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

    1

执行以上一行代码可返还所要查询的array数组中对象value属性的最大值100。
同理,要查找最小值如下即可:

Math.min.apply(Math, array.map(function(o) {return o.value}))

    1

是不是比for循环方便了很多。

posted @ 2019-06-05 14:08  KJXY  阅读(147)  评论(0)    收藏  举报