获取JSON数组中的最大值

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

[ { "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" } ] 

通过数组获取最大值然后从此值获取值“player”和“team”的最佳方法是什么? 该页面将是交互式的,因为我将有一个下拉菜单栏,允许查看者在“玩家”和“团队”之外的六个JSON值中选择一个。 提前致谢!

只需在arrays中循环,并随时跟踪最大值:

 function getMax(arr, prop) { var max; for (var i=0 ; i parseInt(max[prop])) max = arr[i]; } return max; } 

用法如下:

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

小提琴演示

编辑

您还可以使用Javascript “sort”方法获取前n个值:

 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); } 

用法:

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

小提琴演示

 function getMaxOfJson(jsonalreadyparsed, property) { var max = null; for (var i=0 ; i max){ max = jsonalreadyparsed[i][property]; } } } return max; } 

这对我有用。

您可能会发现此sortByAttribute函数很有用。 只需按字符串传递属性即可对其进行排序,并且它将返回任何具有您正在查找的特定属性的最大值的对象。 它仍将返回整个数组,只是按您指定的属性进行升序排序。

 var myArr = [ { "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" } ] function sortByAttribue(arr, attribute) { return arr.sort(function(a,b) { return a[attribute] < b[attribute]; }); } sortByAttribue(myArr, "3pg") // returns Carmelo Anthony first sortByAttribue(myArr, "bpg") // returns Anthony Davis first 

这应该工作:

 var highestValue = 0; //keep track of highest value //loop through array of objects for (var i=0, len = ary.length; i highestValue) { highestValue = value; } } 

我发现以下方法非常简洁:

 var arr =[ { "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" } ] console.log( arr.sort( function(a, b) { return parseFloat(b['ppg']) - parseFloat(a['ppg']); } )[0]['player'] ); 

正在寻找具有特定属性’x最大值的项目的函数:

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

用法:

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

这将允许您选择您想要的统计数据以及您想要的信息。

http://jsbin.com/tudegofa/1/edit

data =>是数组

stat =>是您要排序的统计信息

info =>是要返回的属性数组。

 function getValues (data, stat, info) { var selectedValues = data.map(function(x) { return parseFloat(x[stat]); }) var i = selectedValues.indexOf(Math.max.apply(Math, selectedValues)); var result = {}; info.forEach(function(x) { result[x] = test[i][x]; }) return result; } var myData = ''; $.getJSON('/url/to/grab/json', function(data) { myData = data; }); getValues(myData, "bpg", ["player","team"]); //[object Object] { // player: "Anthony Davis", // team: "New Orleans Pelicans" // } 

从长远来看,简单易用。

 function getMaxValueByAttribute(arr, attr) { var max = "-99999999999"; arr.forEach(function (member, index) { // console.log(member, index); if (member.hasOwnProperty(attr) && parseFloat(member[attr]) > parseFloat(max)) { max = member[attr]; // console.log("Max now: " + max); } }); return max; } 

然后使用它像:

 var result = getMaxValueByAttribute(arr, "ppg"); // result = "27.4"