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
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<array.length; i++) {
var item = array[i];
if(item[propName] > max) {
max = item[propName];
maxItem = item;
}
}
return maxItem;
}
usage:
$(document).ready(function() {
$('#getMaxBtn').click(function() {
var max = getMax(jsonArray, 'ppg');
alert(max.player);
});
});
This should work:
var highestValue = 0; //keep track of highest value
//loop through array of objects
for (var i=0, len = ary.length; i<len; i++) {
var value = Number(ary[i]["ppg"]);
if (value > highestValue) {
highestValue = value;
}
}
You might find this sortByAttribute
function useful. Just pass in the attribute by string you're looking to sort it by, and it'll return whatever object has the max value for the specific attribute you're looking for. It'll still return the whole array, just sorted ascending by the property you specified.
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
function getMaxOfJson(jsonalreadyparsed, property) {
var max = null;
for (var i=0 ; i<jsonalreadyparsed.length ; i++) {
if(max == null){
max = jsonalreadyparsed[i][property];
} else {
if (parseFloat(jsonalreadyparsed[i][property]) > max){
max = jsonalreadyparsed[i][property];
}
}
}
return max;
}
This works for me.
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
You can do it by lodash so easily.
var players = [{
"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"
}
];
var topscorer = _
.chain(players)
.sortBy('ppg')
.reverse()
.map(function(o) {
return 'Top scorer: ' + o.player + ' - ' + o.team;
})
.head()
.value();
console.log(topscorer);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.min.js"></script>
Or even shorter:
var players = [{
"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(_.maxBy(players, 'ppg').player);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>