9

Please consider these Key-Value Pairs:

var dict_Numbers = {"96": "0",
                    "97": "1",
                    "98": "2",
                    "99": "1",
                    "100": "4",
                    "101": "0"}

I would like to get the highest value - in this example it would be 101.

How can I achieve this?

Thanks


Update 1:

I use this code: Fast way to get the min/max values among properties of object and Getting key with the highest value from object

but both return Max Value from string comparator

Vlad L
  • 1,544
  • 3
  • 6
  • 20
Arian
  • 12,793
  • 66
  • 176
  • 300
  • With 4k rep you should know to write a [mcve] showing what you have tried. It is really not difficult to find examples here - Math.max and Object.keys – mplungjan Dec 28 '16 at 08:40
  • Possible duplicate of [Getting key with the highest value from object](http://stackoverflow.com/questions/27376295/getting-key-with-the-highest-value-from-object) except that you've to use `parseInt` to convert the string to an integer. – Arun Kumar Mohan Dec 28 '16 at 08:42
  • @ArunKumar No That code compare values not keys – Arian Dec 28 '16 at 08:43
  • Can't you figure out how to compare keys instead of values from the answer I linked? See @mplungjan's answer. He has exactly done that. – Arun Kumar Mohan Dec 28 '16 at 08:47
  • look at this [example]: http://stackoverflow.com/questions/1669190/javascript-min-max-array-values – MKAD Dec 28 '16 at 08:49

5 Answers5

14

Nice example from MDN:

var dict_Numbers = {"96": "0",
                    "97": "1",
                    "98": "2",
                    "99": "3",
                    "100": "4",
                    "101": "5"}
                    
                    
function getMax(obj) {
  return Math.max.apply(null,Object.keys(obj));
}
console.log(getMax(dict_Numbers));
mplungjan
  • 169,008
  • 28
  • 173
  • 236
sinisake
  • 11,240
  • 2
  • 19
  • 27
9

Applying to the keys the easily found Getting key with the highest value from object paying attention to the strings

const dict_Numbers = {
    "96": "0",
    "97": "1",
    "08": "8", // just to make sure
    "09": "9", // just to make sure
    "98": "2",
    "99": "3",
    "100": "4",
    "101": "5"
  },
  max = Object.keys(dict_Numbers)
  .reduce((a, b) => +a > +b ? +a : +b)
console.log(max)

But as I commented on the question, there is a neater way using Math.max on the Object.keys

Now even more elegant using spread

const dict_Numbers = {
    "96": "0",
    "97": "1",
    "08": "8", // just to make sure
    "09": "9", // just to make sure
    "98": "2",
    "99": "3",
    "100": "4",
    "101": "5"
  },
  max = Math.max(...Object.keys(dict_Numbers))
console.log(max)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
4

var dict_Numbers = {"96": "0",
                    "97": "1",
                    "98": "2",
                    "99": "3",
                    "100": "4",
                    "101": "5"}

console.log(Math.max(...Object.keys(dict_Numbers)));

Note that this code uses ES6 features.

str
  • 42,689
  • 17
  • 109
  • 127
  • Cool but the `return Math.max.apply(null,Object.keys(obj));` is by far easier on the eye and brain – mplungjan Dec 28 '16 at 08:59
  • @mplungjan Actually, I think it is the other way around :) – str Dec 28 '16 at 09:02
  • 1
    I just removed the explicit `parseInt` because `Math.max` already does it. But in my opinion, reading the spread operator is easier than `.bind` anyway. – str Dec 28 '16 at 09:05
  • Using a transpiler should be standard practice by now :) – str Dec 28 '16 at 09:17
1

Try this.

You can iterate over the properties of the object and check for its value.

var dict_Numbers = {
  "96": "0",
  "97": "1",
  "98": "2",
  "99": "3",
  "100": "4",
  "101": "5"
};

var max = 0;

for (var property in dict_Numbers) {
  max = (max < parseFloat(property)) ? parseFloat(property) : max;
}

console.log(max);
Arian
  • 12,793
  • 66
  • 176
  • 300
Deep
  • 9,594
  • 2
  • 21
  • 33
  • 1
    Seriously @Arian, that is the answer you went with? At least add the radix to the parseInt or you will have fun with 08 and 09 – mplungjan Dec 28 '16 at 09:00
  • @mplungjan , dont yo think providing a default value is overkill, by default radix is 10 for parseInt() . May be i am wrong but it looks straightforward overkill but i do like sinisake's answer though – Deep Dec 28 '16 at 09:01
  • Right - so they fixed that. It used to give surprises with the invalid octal values 08 and 09 – mplungjan Dec 28 '16 at 09:06
  • yeah but that will affect if try to parseInt a digit i.e. parseInt(015) which results 13 but with strings i.e. parseInt("015") it will not matter. – Deep Dec 28 '16 at 09:16
  • @Arian - why change to parseFloat? Will there be decimals in your "keys" ? – mplungjan Dec 29 '16 at 07:03
0

var dict_Numbers = {
    "96": "0",
    "97": "1",
    "98": "2",
    "99": "3",
    "100": "4",
    "101": "5"
  },
  key,
  intKey,
  maxKey = 0;

for (key in dict_Numbers) {
  intKey = parseInt(key);
  if (intKey > maxKey) {
    maxKey = intKey;
  }
}
console.log(maxKey);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Aditya Singh
  • 116
  • 5