Get the array key with the highest value in javascript


Get the array key with the highest value in javascript

arr[1] = 143;
arr[2] = 25;
arr[3] = 196;

arr[40] = 106;
arr[41] = 16;
arr[42] = 175;

var max_index = -1;
var max_value = Number.MIN_VALUE;
for(var i = 0; i < arr.length; i++)
{
        if(arr[i] > max_value)
        {
            max_value = arr[i];
            max_index = i;
        }
}

If the array is not ordered you cannot do this without iterating.

If you have any query then feel free to contact me at Jainish Senjaliya

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s