I'm trying to get the maximum value of an element from an array of objects:
var graph = {};
graph.data = [
{x: 2008, y: 7.2},
{x: 2009, y: 8.1},
{x: 2010, y: 7.7},
{x: 2011, y: 6.8},
{x: 2012, y: 6.7}
];
to get the maximum, I use the following:
var highest = graph.data[0].y;
for (var i = graph.data.length - 1; i >=1; i--) {
highest = Math.max(highest, graph.data[i].y);
}
which gives me the correct result. However when I try to make a higher order function, so I can reuse it:
var getMax = function (arr, para) {
var highest = arr[0].para;
for (var i = arr.length; i >= 1; i--) {
highest = Math.max(highest, arr[i].para);
}
}
and I try to use it like this:
getMax(graph.data, y);
I get an error, saying Uncaught: "Reference Error: y is not defined", any ideas?
I did try to use the bracket notation instead of the dot one for para, it didn't work