admin管理员组

文章数量:1181439

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

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

Share Improve this question edited Jul 5, 2021 at 17:48 Vlad L 1,6853 gold badges9 silver badges22 bronze badges asked Dec 28, 2016 at 8:37 DooDooDooDoo 13.4k70 gold badges189 silver badges316 bronze badges 5
  • With 4k rep you should know to write a minimal reproducible example showing what you have tried. It is really not difficult to find examples here - Math.max and Object.keys – mplungjan Commented Dec 28, 2016 at 8:40
  • Possible duplicate of 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 Commented Dec 28, 2016 at 8:42
  • @ArunKumar No That code compare values not keys – DooDoo Commented Dec 28, 2016 at 8: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 Commented Dec 28, 2016 at 8:47
  • look at this minimal reproducible example: stackoverflow.com/questions/1669190/… – MKAD Commented Dec 28, 2016 at 8:49
Add a comment  | 

5 Answers 5

Reset to default 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));

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)

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.

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);

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);

本文标签: Get Max Key in KeyValue Pair in JavaScriptStack Overflow