JavaScript
Article
By Sam Deering

jquery get the highest value in array for specific index

By Sam Deering
Last chance to win! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

Simple jQuery code snippet to get the highest value in array for specific index value.

The Dataset.

var data = Array();
data[0] = {"apples":1, "pears":2, "oranges":3};
data[1] = {"apples":3, "pears":3, "oranges":5};
data[2] = {"apples":4, "pears":1, "oranges":6};

The function.

//get the highest value in array for specific index
//usage: getHighestVal(data,index)
//data = array
//index = index of array to analyse
function getHighestVal(data, index)
{
$.each(data, function (i,v)
{
thisVal = v[index];
max = (max The Example:
So in this example, we want to get the highest value of “apple”.
highest value in array for specific index

--ADVERTISEMENT--

Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?