3

I am using this code to get key from array with highest value

function getHighest(o){
    var vals = [];
    for(var i in o){
        vals.push(o[i]);
    }

    var max = Math.max.apply(null, vals);

    for(var i in o){
        if(o[i] == max){
            return i;
        }
    }
}

but sometimes there are more results in array with the same highest value e.g.

item1 = 4
item2 = 2
item3 = 1
item4 = 4

and code I am using returns only first result (item1). So my goal is to get key with highest value but in case there are more elements with the same highest value choose randomly one of them.

rrk
  • 15,677
  • 4
  • 29
  • 45
Kaziko
  • 142
  • 10
  • Store the maximums in another array and look at this: [Get random item from JavaScript array](https://stackoverflow.com/questions/5915096/get-random-item-from-javascript-array) – Malte Hartwig Feb 12 '18 at 08:22
  • There's no need for your initial loop. – T.J. Crowder Feb 12 '18 at 08:22
  • You want highest value or key of element with highest value ? – pixlboy Feb 12 '18 at 08:23
  • *"So my goal is to get key with highest value but in case there are more elements with the same highest value choose randomly one of them."* With an array of simple values as shown, it doesn't matter. Presumably your array isn't just simple values? (In which case, that `Math.max` thing won't work.) – T.J. Crowder Feb 12 '18 at 08:25
  • Check my answer below. 2 methods used in just 2 steps. – pixlboy Feb 12 '18 at 08:34

3 Answers3

1

Here's one approach:

  1. Determine the max:

    var max = Math.max.apply(Math, o);
    
  2. Create an array of only max values:

    var maxes = o.filter(function(val) { return val == max; });
    
  3. Pick randomly from maxes as per this question's answers.

So my goal is to get key with highest value but in case there are more elements with the same highest value choose randomly one of them.

With an array of simple values as shown, it doesn't matter; you could just take the first one, and there's no observable difference between that and any of the others. So presumably your array isn't just simple values — if that's the case, just FWIW, you'll need to find max another way.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Save all index on an array. If only one, send element 0. If multiple return a random element from array.

function getHighest(o) {
  var vals = [];
  for (var i in o) {
    vals.push(o[i]);
  }

  var max = Math.max.apply(null, vals);

  var max2 = []
  for (var i in o) {
    if (o[i] == max) {
      max2.push(i); /* Push the index to temp array */
    }
  }

  if (max2.length == 1) return max2[0]; /* If one element, return first element */
  else return max2[Math.floor(Math.random() * max2.length)]; /* If multiple, select random */
}

console.log(getHighest([1, 2, 3, 4, 2, 3, 4]));
Eddie
  • 26,593
  • 6
  • 36
  • 58
  • 1
    Thanks guys but as I am not good at this stuff I'll stick to out of the box solution. Thank you for all your help. – Kaziko Feb 12 '18 at 08:39
  • 1
    You don't need to test `if (max2.length === 1) return max2[0];`, as `Math.floor(Math.random());` returns 0. But maybe you want to return something `if max2.length === 0` – scraaappy Feb 12 '18 at 11:17
0

You can do it like

 function getHeighest(arr){
    var temp_max = Math.max.apply(null,arr);
    for(var i in arr){
      if(arr[i] == temp_max){
        return i;
      }
    }
  }
  
  var my_arr = [2,4,1,0,3,4];
 
  console.log(getHeighest(my_arr));
Abdullah Shoaib
  • 416
  • 1
  • 5
  • 18