获取 JSON 数组中的最大值 [英] Getting max value(s) in JSON array

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

问题描述

我正在尝试创建一个 JavaScript 函数,该函数从外部 JSON 中的数组获取信息,然后获取其中一个 JSON 变量的最大值(或前 5 个值).对于此示例,假设我想获取值ppg"的最大值.这是数组的一个小样本:

<预><代码>[{球员":安德烈德拉蒙德","team" : "底特律活塞队","ppg" : "15.4",RPG":11.6","apg" : "2.4","bpg" : "1.6",spg":0.8",3pg":0.1"},{球员":安东尼戴维斯",团队":新奥尔良鹈鹕","ppg" : "16.4",RPG":13.6","apg" : "2.6","bpg" : "3.5","spg" : "1.2",3pg":0.1"},{球员":卡梅隆安东尼","team" : "纽约尼克斯队","ppg" : "27.4",RPG":5.4","apg" : "4.5","bpg" : "1.1","spg" : "1.5",3pg":1.6"}]

遍历数组以获取最大值然后从该值中获取值玩家"和团队"的最佳方法是什么?该页面将是交互式的,因为我将有一个下拉菜单栏,允许查看者在除玩家"和团队"之外的六个 JSON 值之一之间进行选择.提前致谢!

解决方案

只需循环遍历数组,并随时跟踪最大值:

function getMax(arr, prop) {最大无功;for (var i=0 ; i parseInt(max[prop]))最大 = arr[i];}返回最大值;}

用法如下:

var maxPpg = getMax(arr, "ppg");console.log(maxPpg.player + " - " + maxPpg.team);

小提琴演示

编辑

您也可以使用 Javascript "sort" 获取前 n 个值的方法:

function getTopN(arr, prop, n) {//排序前先克隆,保留原数组var clone = arr.slice(0);//降序排序克隆排序(函数(x,y){if (x[prop] == y[prop]) 返回 0;否则 if (parseInt(x[prop]) < parseInt(y[prop])) 返回 1;否则返回-1;});返回 clone.slice(0, n || 1);}

用法:

var topScorers = getTopN(arr, "ppg", 2);topScorers.forEach(function(item, index) {console.log("#" + (index+1) + ":" + item.player);});

小提琴演示

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 variables. For this example, let's say I want to get the max value for the value "ppg". Here is a small sample of the array:

[
{
    "player" : "Andre Drummond",
    "team" : "Detroit Pistons",
    "ppg" : "15.4",
    "rpg" : "11.6",
    "apg" : "2.4",
    "bpg" : "1.6",
    "spg" : "0.8",
    "3pg" : "0.1"
},
{
    "player" : "Anthony Davis",
    "team" : "New Orleans Pelicans",
    "ppg" : "16.4",
    "rpg" : "13.6",
    "apg" : "2.6",
    "bpg" : "3.5",
    "spg" : "1.2",
    "3pg" : "0.1"
},
{
    "player" : "Carmelo Anthony",
    "team" : "New York Knicks",
    "ppg" : "27.4",
    "rpg" : "5.4",
    "apg" : "4.5",
    "bpg" : "1.1",
    "spg" : "1.5",
    "3pg" : "1.6"
}
]

What would be the best way to go through the array to get the max value and then get the values "player" and "team" from this value? The page will be interactive, as I will have a drop-down menu bar with allows the viewer to choose between one of the six JSON values aside from "player" and "team". Thanks in advance!

解决方案

Just cycle through the array, and keep track of the max as you go:

function getMax(arr, prop) {
    var max;
    for (var i=0 ; i<arr.length ; i++) {
        if (max == null || parseInt(arr[i][prop]) > parseInt(max[prop]))
            max = arr[i];
    }
    return max;
}

Usage is like:

var maxPpg = getMax(arr, "ppg");
console.log(maxPpg.player + " - " + maxPpg.team);

Fiddle demo

Edit

You can also use the Javascript "sort" method to get the top n values:

function getTopN(arr, prop, n) {
    // clone before sorting, to preserve the original array
    var clone = arr.slice(0); 

    // sort descending
    clone.sort(function(x, y) {
        if (x[prop] == y[prop]) return 0;
        else if (parseInt(x[prop]) < parseInt(y[prop])) return 1;
        else return -1;
    });

    return clone.slice(0, n || 1);
}

Usage:

var topScorers = getTopN(arr, "ppg", 2);
topScorers.forEach(function(item, index) {
    console.log("#" + (index+1) + ": " + item.player);
});

Fiddle demo

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

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