Getting key with the highest value from object

Javascript

Javascript Problem Overview


I have a object like that one:

Object {a: 1, b: 2, undefined: 1} 

How can I quickly pull the largest value identifier (here: b) from it? I tried converting it to array and then sorting, but it didn't work out, since it got sorted alphabetically (and it seems like a overkill to juggle data back and forth just for getting one value out of three).

Javascript Solutions


Solution 1 - Javascript

For example:

var obj = {a: 1, b: 2, undefined: 1};

Object.keys(obj).reduce(function(a, b){ return obj[a] > obj[b] ? a : b });

In ES6:

var obj = {a: 1, b: 2, undefined: 1};

Object.keys(obj).reduce((a, b) => obj[a] > obj[b] ? a : b);

Solution 2 - Javascript

Using Underscore or Lo-Dash:

var maxKey = _.max(Object.keys(obj), function (o) { return obj[o]; });

With ES6 Arrow Functions:

var maxKey = _.max(Object.keys(obj), o => obj[o]);

jsFiddle demo

Solution 3 - Javascript

Here is a suggestion in case you have many equal values and not only one maximum:

    const getMax = object => {
        return Object.keys(object).filter(x => {
             return object[x] == Math.max.apply(null, 
             Object.values(object));
       });
    };

This returns an array, with the keys for all of them with the maximum value, in case there are some that have equal values. For example: if >const obj = {apples: 1, bananas: 1, pears: 1 }
//This will return ['apples', 'bananas', 'pears']

If on the other hand there is a maximum: >const obj = {apples: 1, bananas: 2, pears: 1 }; //This will return ['bananas']
>---> To get the string out of the array: ['bananas'][0] //returns 'bananas'`

Solution 4 - Javascript

Supposing you've an Object like this:

var obj = {a: 1, b: 2, undefined: 1}

You can do this

var max = Math.max.apply(null,Object.keys(obj).map(function(x){ return obj[x] }));
console.log(Object.keys(obj).filter(function(x){ return obj[x] == max; })[0]);

Solution 5 - Javascript

{a: 1, b: 2, undefined: 1}

The best work around I've seen is this

const chars = {a: 1, b: 2, undefined: 1}

//set maximum value to 0 and maxKey to an empty string
let max = 0;
let maxKey = "";

for(let char in chars){
  if(chars[char]> max){
    max = chars[char];
    maxKey= char
  }
}

console.log(maxKey)

Solution 6 - Javascript

Very basic method. might be slow to process

var v = {a: 1, b: 2, undefined: 1};

function geth(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;
        }
    }
}

console.log(geth(v));

Solution 7 - Javascript

Combination of some ideas from other answers. This will get all the keys with the highest value, but using the spread operator to get the maximum value and then filter array method:

const getMax = object => {
  let max = Math.max(...Object.values(object))
  return Object.keys(object).filter(key => object[key]==max)
}

let obj = {a: 12, b: 11, c: 12};
getMax(obj)

Solution 8 - Javascript

let data = {a:1,b:2,undefined:3}
let maxValue = Object.entries(data).sort((x,y)=>y[1]-x[1])[0]

note: this is a very expensive process and would block the event loop if used with objects of large sizes(>=1000000). with large array slice the entries and call the above method recurrently using setTimeout.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomek BuszewskiView Question on Stackoverflow
Solution 1 - JavascriptCD..View Answer on Stackoverflow
Solution 2 - JavascriptFaris ZacinaView Answer on Stackoverflow
Solution 3 - JavascriptpolycconView Answer on Stackoverflow
Solution 4 - JavascriptAmit JokiView Answer on Stackoverflow
Solution 5 - JavascriptRidwan AjibolaView Answer on Stackoverflow
Solution 6 - JavascriptastroanuView Answer on Stackoverflow
Solution 7 - Javascriptivan milenkovicView Answer on Stackoverflow
Solution 8 - JavascriptLandry PlacidView Answer on Stackoverflow