Getting max value(s) in JSON array

前端 未结 11 1689
醉梦人生
醉梦人生 2020-12-06 05:56

I\'m trying to create a JavaScript function which takes information from an array in an external JSON and then takes the max value (or the top 5 values) for one of the JSON

11条回答
  •  伪装坚强ぢ
    2020-12-06 06:16

    function that's looking for item with the specific property'x maximum:

    function getMax(array, propName) {
        var max = 0;
        var maxItem = null;
        for(var i=0; i max) {
                max = item[propName];
                maxItem = item;
            }
        }
    
        return maxItem;
    }
    

    usage:

    $(document).ready(function() {
        $('#getMaxBtn').click(function() {
            var max = getMax(jsonArray, 'ppg');
    
            alert(max.player);
        });
    });
    

提交回复
热议问题